1.2 Variables and data typesEvery value has a type

A data type is a set of values plus the operations you can do on them. Java has two families. Primitive types are built into the language and hold simple values. The three you need are int (whole numbers), double (numbers with a decimal part) and boolean (exactly true or false). Reference types are everything else: String, and every class anyone writes. A variable of a reference type doesn't hold the object itself. It holds a reference, which you can picture as an arrow pointing to where the object lives in memory.
| Type | Holds | Literals | Kind |
|---|---|---|---|
| int | Whole numbers | 42, 0, -7 | Primitive |
| double | Numbers with decimals | 3.75, 2.0, -0.5 | Primitive |
| boolean | Exactly true or false | true, false | Primitive |
| String | Text | "Ana", "", "42" | Reference (a class) |
?Wait, what's an integer again?
A variable is a named storage location. Before you use one, you declare it: you give its type and its name, like int score;. Usually you also give it a starting value in the same line: int score = 42;. A value typed directly into code, like 42, 3.75, true or "Ana", is called a literal.
Names have rules. A variable name starts with a letter, contains only letters, digits and underscores (no spaces), and can't be a Java keyword like int or class. Java is case-sensitive, so score and Score are two different variables. By convention, names start lowercase and capitalize each new word: totalPoints, isOpen. This style is called camelCase.
If a value should never change, add the keyword final: final int MAX_SEATS = 30;. Any later line that tries to assign MAX_SEATS a new value won't compile. Constants are usually written in all capitals with underscores.
?Wait, what's a keyword?
I thought about it. Show me
For each declaration, decide whether it compiles. If it doesn't, say why.
int seats = 30; double price = 5; int count = 2.0; boolean open = "true"; String code = 42; final int MAX = 10; MAX = 12;
- int seats = 30; compiles. 30 is an int literal going into an int variable.
- double price = 5; compiles. Java automatically widens the int 5 into the double 5.0. Going from int to double never loses anything, so Java does it for you.
Why?
Every int has an exact double version, so the conversion is safe. The reverse is not safe, as the next line shows. - int count = 2.0; does not compile. 2.0 is a double literal, and storing a double in an int could lose the decimal part, so Java refuses unless you cast (topic 1.5).
Why?
It doesn't matter that 2.0 happens to be whole. Java checks types, not values. - boolean open = "true"; does not compile. "true" in quotes is a String, not the boolean value true.
- String code = 42; does not compile. 42 is an int, not a String. (String code = "42"; would be fine.)
- final int MAX = 10; compiles, but MAX = 12; does not, because a final variable can't be assigned again.
Answer: Compile: int seats = 30;, double price = 5; and final int MAX = 10;. Don't compile: int count = 2.0;, boolean open = "true";, String code = 42; and MAX = 12;.
Stuck? Get a hint
- For each line, name the type of the variable and the type of the value on the right.
- int to double is automatic. double to int is not.
- Quotes make a String, no matter what is inside them.
double d = 9; System.out.println(d);
- A9
- B9.0
- C9.00
- DNothing: the code does not compile
Show the answer
B. The int 9 is widened to the double 9.0 when it is stored, and printing a double always shows a decimal point. Choice D is the trap: storing an int in a double is allowed. Only the other direction needs a cast.
- Primitive types: int, double, boolean. Reference types: String and every class. A reference variable holds an arrow to an object.
- Declare before use: type, then name. Names are case-sensitive, camelCase by convention, and can't be keywords.
- An int can go into a double automatically. A double can't go into an int without a cast.
- final makes a variable a constant: it can't be assigned again.