1.3 Variables and Data Types
Variables allow us to store information such as numbers, words, or true/false expressions. A variable can be thought of as a box that stores information inside. In Java, variables are composed of three things: a name, type, and value.
In Java you must specify what type of information you want our variables to hold. There are two types that are used in Java: primitive and reference. You must always give your variable a type before naming it, and specifying the value it holds. (Ex. int myVariable = 10;
)
Here are some of the primitive types found in Java:
As seen above, primitive numeric types in Java include both integers and doubles.
Integers are whole numbers, or counting numbers. (Ex. -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5). In Java you declare an integer using int
before the variable name. Here are a couple of examples:
doubles are like integers, but can have decimals. (Ex. -54.34, 90.21, 0.1223). In Java you declare a double using double
before the variable name. Here are a couple of examples:
Characters represent a single character. In Java you declare a character using char
before the variable name. You also use single quotes to identify a character (Ex. ‘A’). Here are a couple of examples:
Booleans are variables that hold a true or a false value. In Java you declare a boolean using boolean
before the variable name. Here are a couple of examples:
While primitive types are considered the building blocks of programming languages, reference types are types that have been created by programmers using those primitive types. String for example, is a variable type that was created using char variable types, and has its own functionality that has been created by programmers.
One of the primary differences between primitive types and reference types is how they are stored in memory. When you assign a value to a variable, you are actually associating that variable with binary data. Computers store all data in binary, and associate that variable with the binary data associated with the value that its been assigned. When primitives are given values, they are associated directly with those values. If the value of one variable changes, then the primitive value that it stores also changes.
In this example, the value of first is being changed to equal the value stored in second. Both first and second now store the value b
.
Reference types store an address that points to where the value is located in memory:
firstStr
and secondStr
initially start out with two separate String values. When firstStr
is set to equal secondStr
, firstStr
does not store the value of Goodbye
, but actually references the exact same copy of the Goodbye
stored in secondStr
. This is the primary difference between primitive and reference types.
firstStr
now references Goodbye
, and Hello
still exists in memory until the computer is able to wipe it from memory. It does so through a process called garbage collection.
Strings are variables that hold text. Strings are not a primitive type, so you must declare them using String with a capital S. Unlike characters, you need to use double quotes when assigning strings (Ex. "This is my string."
). Here are a couple of examples:
Variables can be protected from alteration if the keyword final
is included before the data type. If there is an attempt to change the variable value, then an error will be called indicating that variable has already been assigned. This is often used as a security measure in more complex programs to ensure that values cannot be modified by others.
In this example, the program will throw an error because the value of numApples
cannot be changed if the keyword final is added to the variable declaration.
Giving your variables meaningful names throughout your code is very important. Proper variable names allow others to easily read and understand your code. A good way to name your variables is to give them as descriptive of a name as possible, without making it too long.
For example,int numberOfApplesOnTheTree = 10;
is a very long name, and can easily be replaced with a name like int numApples = 10;
.
Variable Naming Conventions:
- Variable names must start with a letter, $ symbol, or _ symbol.
- Variable names are case sensitive so
myVariable
is different thanMyVariable
- Variable names, after the first character, can contain letters, numbers, or other characters.
Here are some examples of different variable names:
-
Incorrect
Correct
No Answer was selected
Invalid Answer
If you were to create a variable to store your age, which of the following would be best considering the variable type and Java naming conventions?
In this exercise, we’ve started to declare some variables but we don’t know the answers to the questions. You should fill in the rest of the lines to make them correct.
Edit the code so that the output of running the program is: