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 reference data.
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
Each primitive data is provided with a data type, a predefined reserved keyword that indicates what type of data is 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 the data type boolean
is used.
For character values the data type char
is used.
Primitive vs. Reference 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 reference data. Reference data are used to represent or simulate something that is part of the real-world, like dice or calendar dates. They can also be used to group related data together. Reference data is represented with classes, which are blue prints that specify the data stored and define any behaviors or functions 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
: A prime example of reference 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 is an existing class that can be used to represent dates called the
LocalDate
class. This class contains the data for the calendar date (month, day, year) and defines behaviors that allow you to manipulate these dates, such as making it reoccuring year to year.
- 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 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 spread sheet, 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.
Let's do some practice
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, we identify which of the 4 data types would be best to use to represent the data. The 4 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 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 either 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