We use variables to store data in order to manipulate or change our program. A variable in a named location in memory that stores a specific value. We can use the name to reference this value. Values can be numbers, text, objects, images or other types of data.
To create and use a variable, they require a data type, name and an assignment.
This is commonly referred to as declaring and initializing a variable.
Declaring a variable takes two components: a data type and a name of the variable that the developer creates.
Initialization means that the developer provides an initial value for the variable.
Over the next few pages, we will break apart these tasks and explain them in more details.
Data Types
A data type is an attribute of data which tells the compiler how the programmer intends to use the data. A data type constrains the values that a variable might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. (adaptive from https://en.wikipedia.org/wiki/Data_type)
Java has eight primitive data types. Java also uses reference or class data types, which is what truly gives Java the object oriented power it holds.
Primitive Data Types
Java’s eight primitive data types are:
- int
- double
- long
- short
- float
- char
- byte
- boolean
TutorialsPoint.com does an excellent job at summarizing the difference between all of these data types and provides an example of how to declare and initialize variables for each of these eight primitive data types. Check it out.
https://www.tutorialspoint.com/java/java_basic_datatypes.htm
Declaring Primitive Variables
A single variable can be declared by using the data type and a great variable name. The Java conventions surrounding variable names will be discussed next.
int age;
int heightInInches;
int numOfKids;
double weight;
double gpa;
Once a variable is declared with a specific data type, it cannot be changed during the program run.
If you have multiple variables that you need to declare of the same type, you can execute the creation of these variables all in one line:
int age, heightInInches, numOfKids;
double weight, gpa;
An important note about variables that are not initialized immediately: variables that are not initialized immediately will hold a default value. For any number data type, this default is 0 or 0.0. For a boolean, the default is false.
Class Data Types
Class variables are declared and initialized using constructors. A constructor uses the new
keyword to create an instance of the class (known as an object). A simple class data type that we will be using shortly is a Scanner. The Scanner is a package in Java that lets us interact with different streams to input data. We will use the Scanner to open a connection to the console in our Java programs in order to get user input. To use the Scanner class, we create an object of the class and specify where the scanner should listen for input. The Scanner class has predefined constructors that we will utilize to open this connection.
Scanner in = new Scanner(System.in);
When we want to have the Scanner provide input back into our program, we reference the Scanner using the local name we provided it with: in
In this example, Scanner in the data type for our object, in
is the local variable name and the right side of the equal sign uses the constructor to initialize the Scanner. All the same functionality to declare and initialize a variable for use, whether it is a primitive or class data type.
In addition to using classes include in Java, we can create our own classes to hold object data as well. For example, if I needed a program to hold Cupcake information, I can create a Cupcake class to hold details for cupcakes and create objects for individual cupcakes. My Cupcake class might hold flavor, icingType and toppings. I could create cupcakes from my class to hold my favorite cupcake details.
Cupcake myFavorite = new Cupcake("vanilla","buttercream","sprinkles");
We will revisit this concept of using built-in class data types shortly with the String and Scanner class. Later, we will create our own classes and objects.
What is a primitive vs a class data type?
Have you ever been camping? What does this have to do with Java??
Compare a primitive datatype to a primitive campsite. What do you get if you have a primitive campsite? Usually, just ground to put a tent on. Sometimes you might get a picnic table or even some gravel to park your car. But generally, it is just basic land for you to pitch a tent.
Now, consider a luxury Class A campsite. Bells and whistles. Not just electricity, but your choice of amperage (15 amp, 30 amp service or 50 amp service), water at the site, sewer hookup. Some sites have cable and wifi. Even a concrete pad so your big rig stays level. Maybe even patio furniture.
If you are going camping, both campsites will get the job done. But if depending on your camping needs, you may not need a luxury class A campsite.
How does this relate to data types? The eight primitive built-in data types in Java are like your basic primitive campsite. Very low overhead for use. Built in and ready to go. Not a lot of bells and whistles. But they will complete the goal of camping.
On the other hand, reference or class data types have a lot of benefits that come with using them. However, they also have a lot of baggage and cost us more to use in our programs.
Which type is the right one to use? It depends on the situation. If the program just needs to complete simple math or need a simple true/value value, go for the simplest data type out there: the primitive data types. Generally, any time that you can opt for primitive data types, the program will have less overhead and will in turn run faster and be less complex to maintain.
Up Next: Constant Variables