Variables and the primitive types
Giving values a name, the primitive types you will use every day, and what really happens when you divide a whole number or cast one.
A name for a value
A variable is a name for a value. You declare one by writing the type, then the name, then the value: int minutes = 90;. From that line on, minutes means 90, and you can read it, print it, or give it a new value with a plain equals sign.
Java insists on the type from the start. It will not quietly put a word into a variable that holds a number, and it never guesses what you meant. That strictness feels slow for a week, and after that it starts catching mistakes before the program ever runs.
The types you will use
int holds a whole number. double holds a number with a fraction. boolean is either true or false. char is one character in single quotes. long is an int with more room inside, written with a capital L at the end of the value.
Text is not a primitive. A String is an object — a group of characters with methods attached — which is why the next chapter is about it. Writing "42" in quotes makes text and writing 42 makes a number, and the two behave differently in arithmetic and in output.
Whole numbers, and the plus sign
Dividing two ints gives an int, so 90 / 4 is 22 and the remainder is dropped. Make either side a double and the fraction survives: 90.0 / 4 gives 22.5. This is the most common surprise in the language, and it is a rule rather than a bug.
A cast asks for a type on purpose: (int) 4.9 is 4, because a cast toward a whole number cuts the fraction off instead of rounding it. The plus sign does two jobs — with numbers it adds, with text on either side it joins the pieces into one longer string, working from left to right.
What a type promises
The type is a promise about the shape of the value and the space it needs. An int is 32 bits and holds roughly minus two billion to two billion; a long is 64 bits and holds far more. Go past the edge of an int and it wraps around to the other end instead of stopping, so the largest int plus one is negative.
double is a binary floating-point number, which means it can hold 0.5 exactly and 0.1 only approximately. That is fine for measurements and wrong for money, where whole cents keep the arithmetic exact.
char is a single UTF-16 code unit in single quotes, and boolean has exactly two values. String looks like the others but is a class, so its values are objects and its name is capitalised.
Narrowing, widening and final
Putting an int into a double is widening: nothing is lost, so Java does it without being asked. Putting a double into an int is narrowing, and Java requires the cast because the fraction will be cut off.
final marks a name that may be assigned once. It is not the same as a compile-time constant, but it is the right way to say that a value should not change after it is set. The compiler then catches a second assignment instead of letting it pass.
Names, and the shape of a declaration
A declaration reads left to right: the type, the name, an equals sign, the value. The name is yours to choose. It may not be a keyword such as int or class and it may not contain a space, and by convention it starts lowercase and capitalises each later word, as in secondsPerYear.
Java also refuses to read a local variable that has never been given a value. int total; System.out.println(total); does not print zero and does not compile, because the compiler follows every path to the read and will not let one arrive without an assignment. A field of a class starts at zero instead, and mixing the two rules up is a common early surprise.
Declaration, division and casting
int minutes = 90;
double price = 4.5;
boolean paid = false;
char grade = 'A';
long secondsPerYear = 31536000L; // the L marks a long
System.out.println(minutes / 4); // 22
System.out.println(minutes / 4.0); // 22.5
System.out.println((int) 4.9); // 4, not 5
System.out.println("total " + 1 + 2); // total 12
System.out.println("total " + (1 + 2)); // total 3One value of each type
Press Run and check every line against the value in the code. Then add a variable of your own, print it, and try dividing it by 4 with and without a decimal point.
Three habits prevent most type bugs: do not assume int can hold any whole number, do not compare two doubles for exact equality, and do not use == to compare text. Overflow wraps silently, 0.1 + 0.2 is not exactly 0.3, and two equal strings can still be different objects. Check the range, allow a tolerance, and call equals.
Try these next
- Declare an int at its largest value with
Integer.MAX_VALUE, add one, and explain the negative number you see. - Store a price in cents, print it as a decimal by dividing by
100.0, and keep the cents for the arithmetic. - Make a
final intfor a year, try to assign it a second time, and read the compiler message.
A ticket total, with tax and a cast
Press Run and check each value against the arithmetic. Then change the ticket count and price, predict the total before running, and make the program print the tax with two decimal places.
Watch the plus sign. "joined: " + minutes + 3 glues the digits together and prints joined: 903. Put the sum in round brackets and you get joined: 93 instead.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does 90 / 4 print?
