Variables and Data Types
A Java program keeps its information in variables, and each variable has a data type. Here are four facts a hiking app stores about one trail.
int switchbacks = 14;
double lengthKm = 7.8;
boolean dogsAllowed = true;
String trailName = "Hollow Ridge";
Declaring a variable
Each of those lines is a declaration. A declaration gives a type and then a name, and each of these also gives a starting value after an equal sign. In
int switchbacks = 14; the type is int, the name is switchbacks, and the starting value is 14.The name is the programmer's choice, and later lines use it to reach the value. The type is fixed when the variable is declared. Later in the program,
switchbacks can hold a different whole number, but it can't start holding text.Values and operations
A type decides both halves. The values of
int are whole numbers, positive, negative, or zero. The values of double are numbers that can have a fractional part, like 7.8. A boolean has two values only, true and false.The operations differ too. Adding two
int values is an operation on the int type, so switchbacks + 3 is 17. Arithmetic isn't an operation on boolean values. Since dogsAllowed + 1 asks for one, it doesn't compile.Primitive and reference types
The types
int, double, and boolean are primitive types. A variable of a primitive type holds its value itself: lengthKm holds 7.8.String is a reference type. The text "Hollow Ridge" is an object, stored apart from the variable. The variable trailName holds a reference to that object, a way of finding it, and not the characters. Objects and references get their own lessons later in this unit.Java has five more primitive types, such as
long and char. This course doesn't use them, and they aren't on the exam.A value that fits the type
The compiler checks each declaration's starting value against its type. Each line below has a value of the wrong type.
int switchbacks = 14.5;
boolean dogsAllowed = 1;
Neither line compiles. The literal 14.5 is a
double, and an int variable accepts only int values. Even 14.0 is refused there, since it's still a double literal. A boolean holds true or false, and 1 is a number, so the second line fails too.The other direction works. In
double lengthKm = 8; the whole number 8 is also a value a double can hold, so the line compiles and lengthKm holds 8.0.Declared before use
A variable exists only from its declaration onward. Java reads a code segment from the top, so a line can use a name only if the declaration came earlier.
double plannedKm = lengthKm;
double lengthKm = 7.8;
The first line asks for the value of
lengthKm before any variable has that name, so the segment doesn't compile. Swapping the two lines fixes it, and then plannedKm starts at 7.8.Choosing a type
What a program has to store usually decides the type. A count, like the switchbacks, is a whole number, so it's an
int. A measurement like the trail's length can fall between whole numbers, so it's a double. A yes-or-no fact, like whether dogs are allowed, is a boolean, and a name is text, which is a String.