AP Computer Science A · Unit 1 lecture · Part 2 of 11 · 7 min

1.2 Variables and data typesEvery value has a type

Mr. Thompkins
Before Java stores anything, it wants to know what kind of thing it is. A whole number? A decimal? A yes-or-no? Get used to asking "what type is this?" about every value you see. It answers half the questions on this exam.

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.

TypeHoldsLiteralsKind
intWhole numbers42, 0, -7Primitive
doubleNumbers with decimals3.75, 2.0, -0.5Primitive
booleanExactly true or falsetrue, falsePrimitive
StringText"Ana", "", "42"Reference (a class)
The four types you meet first. Notice that 2.0 is a double even though it is a whole number, and "42" is a String, not an int.
?Wait, what's an integer again?
An integer is a whole number: positive, negative or zero, with no fractional part. 7, 0 and -12 are integers. 7.5 is not. In Java, int is the type for integers, and double is the type for numbers that can have a decimal part.

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.

variableswhat it means42int score3.75double pricefalseboolean donethe box holdsthe value itselfString name"Ana"the box holds an arrow
Primitive variables hold their value right in the box. A String variable holds a reference: an arrow to a String object somewhere else in memory. This difference matters a lot once you copy variables in 1.13.
?Wait, what's a keyword?
A keyword is a word that already means something to Java, like int, double, boolean, class, public, new, final and return. You can't use a keyword as a variable name, because Java would not know which meaning you want.
Pause and think
Pick the best type for each: the number of students in a class, a runner's time in seconds measured to the hundredth, and whether a store is open right now.
I thought about it. Show me
int for the number of students (you can't have 23.5 students), double for the time (it has a decimal part, like 12.47), and boolean for whether the store is open (it's either true or false).
Which lines compile?

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;
  1. int seats = 30; compiles. 30 is an int literal going into an int variable.
  2. 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.
  3. 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.
  4. boolean open = "true"; does not compile. "true" in quotes is a String, not the boolean value true.
  5. String code = 42; does not compile. 42 is an int, not a String. (String code = "42"; would be fine.)
  6. 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
  1. For each line, name the type of the variable and the type of the value on the right.
  2. int to double is automatic. double to int is not.
  3. Quotes make a String, no matter what is inside them.
Check yourself
What is printed by this code?
double d = 9;
System.out.println(d);
  1. A9
  2. B9.0
  3. C9.00
  4. 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.