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,
- audio 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:
- These apps take in and process data from the user that is setting up the quiz, maybe the teacher, such as:
- the questions,
- potential answer choices, and
- which answer choice is correct.
- It takes in user data, such as:
- user name,
- answers provided.
- The app produces data, such as:
- a game code to join the game,
- points for the user 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.
Analyzing Data:
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.
Primitive Data
Java divides data into primitive data and reference data.
Primitive data is:
- numbers, which are either integers (positive, negative whole numbers or zero) or real numbers
- whether something is true or false, called boolean values
- characters
Primitive data is provided with a data type. A data type indicates what type of data are to be stored.
- For integer values, there are four data types:
byte, short, int, and long. We will focus on using int.
- For real numbers, there are two data types:
float and double. We will focus on using double.
- For boolean values (
true or false), the data type boolean is used.
- For character values the data type
char is used.
Primitive vs. Reference Data
All other data types are considered reference data. Reference data are used to represent or simulate something that is part of the real-world, like dice or calendar dates.
Reference data is represented with classes, which are blue prints that specify the data stored and define any behaviors that are included. There are existing classes that can be used or you can create your own classes. There will be opportunities for both throughout these materials.
Some examples of reference data are:
String: String data are collections of individual characters, such as words and sentences and would be represented by the String class.
- Dates: A calendar date, such as a birthday can be represented with the
LocalDate class. This class contains the data for the calendar date (month, day, year).
- Dice: Used in games to generate random values between 1 and the number of sides, inclusively. You can create a
Dice class to simulate the rolling of a dice.
- A Color Block: Many games use a color block that you move on the screen (2048, Oh h1, Tetris). Some blocks have numbers or letters on them as well. You can create a
Block class to represent a colored block including the number or letter that can be used as a visual representation in the game.
- Fruit data: From a spreadsheet, it could include the name of the fruit, form it is being distributed in (fresh, canned, juice), retail price, unit of this price, etc. All of the data listed here are
String or primitive data and can represented by creating a Fruit class to store all the data together.
There exists classes that can be used to represent the primitive data types as well:
Integer: Represents integer values
Double: represents real-number values
Boolean: represents boolean values (true or false)
Character: represents character values
Some Examples
- For the data descriptions listed below, first determine whether a primitive data can be used or if it would require the use of a reference data type.
- If a primitive data type can be used, identify which of the 4 data types (
int, double, boolean, char) would be best to use to represent the data. For this exercise, we will not be using the corresponding reference types.
- Some of the descriptions could be represented multiple ways, so be sure to explain your choice.
| Data Description |
Primitve or Reference |
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 an integer is the best choice. |
| 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. |
| Your user name |
Reference |
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. |
| The information stored about a menu item including its name, a description, and the price |
Reference |
Menu |
Since this data is being stored as a collection of related data, we should create a class. |
| 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. |
| 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. |
| Your graduation date |
Reference |
LocalDate |
Since it is a calendar date, it is best to use the LocalDate class. |
| 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
Next Learn Tutorial
Learn: Naming Variables