Learn Java 2: Variables and Data Types
This is the second official article in the Java coding series. The previous article, First Program in Java, can be found at the following link: http://nytjournal.org/articles/learn_java_2
Rather than being a programming tutorial, this article will focus on the theory behind variables, both in Java and in programming in general. We will start by looking at what a variable is, and how to create them in Java. Then, we will learn how Java stores variables, the methods’ advantages/disadvantages, and how to take advantage of it in your programs.
In programming terms, a variable is a way to store a certain piece of data, represented by a name. For example, to store the number 5, I could create a variable named ‘x’. This also works for different types of data; I could store the letter ‘a’ as a variable named ‘character’. Thus, on a very basic level, a variable is simply a reference to a piece of data.
In Java, variable creation is a two step process. First, the variable must be declared, meaning that its name and data type are stated, so Java knows how to create space for the variable behind the scenes. For instance, to declare an integer named ‘x’ in Java:
In this line, first the data type for integers, int, is specified as the type for the variable. Then, its name, x, is written to tell Java how you will refer to this variable in the future.
The next step to creating a variable in initializing it. Initializing a variable is essentially a fancy way of saying that you are assigning it a value, just as you would say “x equals 5” in a math class. Continuing with our above code, assigning ‘x’ a value looks like:
x = 5;
Now, after this code, we will have a variable called ‘x’ that is storing the number 5.
However, although this is a two step process, that does not mean it must take two lines of code to accomplish, however. Declaring and initializing a variable can be done by combining the two above lines of code into a single line:
int x = 5;
As seen through the example, creating a variable follows a specific format. First, you state the data type. Next, the name. Finally, if you are initializing the variable on the same line, you use the equals sign and state the value after. Generalized, this ‘formula’ looks like:
dataType variableName = value;
Now that we know how to create variables, let’s take a look at how they are stored by Java behind the scenes. But first, a look at data types in Java.
*Note: this section of the tutorial may require some previous computer science or programming knowledge, due to the fact it covers technical material*
In order to store variables, Java must know what exactly it is storing, in order to know how to store it. To accomplish this, variables are given a data type to specify what they contain. In Java, data types are split into two categories, primitive data types (known as primitives) and objects/classes.
Primitive data types contain most, if not all, kinds of number available in Java, as well as several other concepts. These include integers, decimals, Booleans (true/false), and characters. Below is a list of all primitives in Java, as well as a short description of what they store:
|Int||Stores an integer value|
|Double||Stores a decimal (floating point) value -64 bits|
|Float||Similar to double what; also stores a floating point value but has less storage capability (only 32 bits)|
|Boolean||Stores a true/false Boolean state|
|Char||Stores a single Unicode character as its ASCII value|
|Short||Stores an integer, but only uses 16 bits|
|Long||Also stores an integer, but uses 64 bits|
|Byte||Also stores an integer, but only uses 8 bits (1 byte)|
The other kind of data is objects, which are used to represent real world objects or concepts (more on classes and objects later).
DATA STORAGE CONTINUED
All primitives are stored using different arrangements of bits. A bit is the fundamental of data storage, and has two states: 1 (memory switch on), and 0 (memory switch off); 8 of these bits equals a byte. Ints, shorts, bytes, and longs use all of their available bits except one to store the number, and the last one to store the sign (positive or negative) of the number. Thus, the amount of possible numbers able to be stored for an integer primitive with n bits of space is 2n - 1.
Every numerical primitive is stores its actual value. Thus, in memory where the variable is kept, you would see a chain of bits representing the value of the variable if you could “look” at the RAM values. However, rather than storing objects by storing its content, variables referring to objects store the memory address “pointing to” the object’s actual contents instead. Thus, variables containing objects do not store the object itself, but rather a “reference” to it (also called a “pointer”).
Both of these approaches to data storage have advantages and disadvantages. Most important among these is what happens when the variable’s data is passed, or “given”, to another variable. With primitives, the actual value or the primitive is copied into the memory slot of the new variable, meaning that modifying one variable will have no effect on the other. This is known as “passing by value”, because the actual value of the variable is being passed from one variable to the other. However, when variables storing objects pass their data to another variable, they do not give the new variable a copy of the object; they copy the memory address of the original object into the new variable. This means that because modifying either variable will modify the object at the memory address that it stores (the same address), modifying one variable has an effect on the other. This other method is known as “passing by reference”, because rather than passing a copy of the object, the variable passes the reference to the original object. This is something to watch for whenever coding with objects, because if the programmer is not careful, an object can be modified where it should not be modified.
To Recap: variables are used to store data not only in Java, but also in programming in general. To create a variable in Java, specify the data type and the name, and use the assignment operator to give the variable a value. There are two broad categories of storable data: primitives and objects. Primitives represent numbers and are passed by value, whereas objects are special constructs used to model the real world and are passed by reference.
Stay tuned for the next tutorial which will be about code structure in Java. It will also lead up to the first lab!
The links to our previous two articles are: