Introduction to Determining Data Types
A computer takes in input (data), stores it, processes it, and then outputs the new data. It's all about data. That is the source and life of any program. Consider some of the following popular programs you might be using:
- Language Learning App: A language learning app takes in and processes data from the user, such as: their account set up information, their responses to language challenges (this could be text or audio). It also needs internal data to run the app, such as: words and phrases in both the native language and the language that's being learned, videos of the language being spoken. It will produce output, such as: whether the user got the answer correct, points for the user, the level that the user is on, a leader board for all users.
- Quiz Games: Many teachers use quiz games for students to practice and study new material. These apps take in and process data from the user that is setting up the quiz, such as: the questions and potential answers, which answer is the correct answer. It takes in data from all the users that are taking the quiz, such as: their user name, the answers they provide. The app would produce data, such as: a game code to join the game, points for the users based on whether their answers are correct as well as how much time it took to answer the questions, a leader board of who is winning and determining the ultimate winner, graphs of how each question was answered and how many people selected each answer choice.
Let's also consider a program that would be used to Analyze Data. The input would be data from a database. The program would store this data in a list. The program would then process the data to answer a question someone might have about the data. The output would be the answer from this analysis.
Java divides data into primitive data and object data.
Primitive Data
Primitive data is:
- numbers
- whether something is true or false, called boolean values
- characters
Numbers are broken into two categories:
- Integers (positive, negative, and zero) - There are four data types:
byte
, short
, int
, and long
.
- Real Numbers - There are two data types:
float
and double
.
Why are there all these data types to represent integers and real numbers?
Each of these data types have a different storage capacity. This can be important if you have storage concerns.
- byte: The
byte
data types is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127(inclusive).
- short: The
short
data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767(inclusive).
- int: The
int
data type is a 32-bit signed two's complement integer. It has a minimum value of -2 to the 31s power and a maximum value of 2 to the 31 power - 1. It can also be used to represent a larger range of unsigned values.
- long: The
long
data type is a 64-bit two's complement integer. It can also be used to be a signed or unsigned value range.
- float: The
float
data type is single-precision 32-bit floating point.
- double: The
double
data type is a double-precision 64-bit floating point.
- boolean: The
boolean
data type has only two values: true
and false
.
- char: The
char
data type is a single 16-bit Unicode character.
Primitive vs. Object Data
What if the data isn't a number, boolean, or character? How do we store it then? Data that doesn't fall into these categories are considered objects. Objects either represent things in the real-world or group related data together. They are represented in one of two ways:
Some examples of object data are:
String
: A prime example of object data would be words, sentences, paragraphs. These collections of individual characters is considered String
data and would be represented by the String
class.
- Dates: A calendar date, such as a birthday. There are ways to manipulate these dates, such as making it reoccuring year to year. We would use the
LocalDate
class to represent dates.
- Dice: Used in games to generate random values between 1 and 6 inclusively. You can create a
Dice
class to simulate the rolling of a dice.
- A Block: Such as the ones in 2048 that have color and a number on them. You can create a
Block
class to represent a colored block with a number on it that can be used as a visual representation in the game.
- Water Quality data: From a spreadsheet, it could include the date of the test, the location, the pH level of the water, the temperature of the water, etc. All of the data listed here are primitive or object data and can be represented in a
WaterQuality
record that stores all the data together.
Let's do some practice
For the data descriptions listed below, we first determine whether it is primitive data or object data. If it is primitive data, we identify which of the 8 data types would be best to use to represent the data. The 8 primitive data types are listed and described above. Some of the descriptions could be represented multiple ways, so be sure to explain your choice.
Data Description |
Primitve or Object |
Data Type |
Explanation |
The quantity of water bottles that are available for sale |
Primitive |
int |
A quantity is a number. There are only whole numbers of water bottles, so it is going to be an integer. A short could also be used, since it is a reasonably larger number to have in inventory. The int type is a common type to use for integers. |
Whether you are paying with cash or credit card |
Primitive |
boolean |
Since the answer to this is either yes or no , that would be equivalent to true for yes and false for no . A case could be made for this being a String for cash or credit card |
Your user name |
Object |
String |
Since your user name is a collection of characters, it would need to be stored in a String . |
The amount of money you get paid per hour at a part-time job |
Primitive |
double |
Since we are using money here, we have real-number data. This data is best represented in a float or a double . You will often see double being used for real-numbers. |
The information stored about a menu item including its name, a description, and the price |
Object |
Menu |
Since this data is being stored as a collection of related data, we should either create a class or a record. |
The letter grade you recieved on a test |
Primitive |
char |
Since this is a letter grade (A , B , C , D , F ), the char type is best. You could also use a String . |
Whether you passed or failed your driving test |
Primitive |
boolean |
Since this is a two option situation, either you passed or you didn't, we can use a boolean type. Other types could also be used, such as char if it was represented as a p for passed or f for failed. You might also choose to use the String type and use the words passed or failed . |
Your graduation date |
Object |
LocalDate |
Since it is a calendar date, it is best to use the LocalDate class. |
Your dessert selection from the choices: brownie, cookie, or cake |
Object |
String |
Since the dessert selections are word a String would work best. You could also choose to number the choices 1 for brownie, 2 for cookie, and 3 for cake. In this case, you would want to use a int or other integer type. Another option would be to have each choice be assigned a letter, so A for brownie, B for cookie, and C for cake. In this case, you would want to use a char or String . |
A language phrase for a language learning app |
Object |
String |
Since this would be more than a single character, it would need to be stored in a String . |
Resources