Variables and the Primitive Types
Where you are: Week 0 review > Variables and the primitive types
Try This First
A friend who knows Python says: “I can write x = 5 and later x = 'hello' and it works.” Decide whether the same two lines work in Java before reading on.
Reveal
No. Once you declare int x = 5;, the name x is permanently an int. Assigning text to it is a compile error. Java fixes the type at declaration and never changes it.
Before You Start
Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.
See How a Java Program Compiles and Runs if either box is unclear.
Not sure? Take the 60-second self-check.
Try each from memory, then read the answer under it.
- Which file does the JVM run, the one you typed or the one the compiler produced? The one the compiler produced, the
.classbytecode. You edit and keep the.javasource. - You change a source file and run it without recompiling. Which version runs? The old one. The launcher runs whatever
.classis on disk, so you compile again first.
What You Need To Walk In With
Walk into the next class able to state these:
- A variable is a labeled box that holds one value of one declared type, and that type cannot change.
- The four CS1 primitive types are
int(whole numbers),double(decimals),boolean(trueorfalse), andchar(one character).Stringholds text and is not a primitive. =means gets, not equals. The right side is computed first, then stored into the name on the left.- A local variable has no default value. Assign a value before you read it.
'A'in single quotes is achar;"A"in double quotes is aString. They are different types.
You should be able to: classify a literal as int, double, char, String, or illegal; trace a short sequence of assignments and state the exact output.
How It Works
Writing int age = 20; does three things at once: it reserves memory for a whole number, labels that memory age, and stores 20 in it. The type label is permanent; the value inside can change.
int age = 20;
double gpa = 3.75;
boolean enrolled = true;
char section = 'A';
String name = "Alex";
| Type | Holds | Example |
|---|---|---|
int |
Whole numbers, about negative two billion to positive two billion | int age = 20; |
double |
Decimal numbers, about 15 significant digits | double gpa = 3.75; |
boolean |
true or false, nothing else |
boolean enrolled = true; |
char |
One character, in single quotes | char section = 'A'; |
String |
Text (an object, not a primitive) | String name = "Alex"; |
= means gets
int x = 5; // x gets 5
x = x + 1; // x gets the old value of x plus 1, so x is now 6
Read = as gets. The right side is evaluated, and the result is stored on the left. It is a command, not a claim that the two sides are equal.
Declaration without a value
int count;
System.out.println(count); // compile error: variable count might not have been initialized
A local variable starts with no value. Assign one (int count = 0;) before you read it.
A constant that does not change
final double TAX_RATE = 0.08;
final marks a value that must not be reassigned. The convention names constants in all capitals.
Worked Example: Predict, Then Check
int x = 10;
x = x + 5;
System.out.println("x: " + x);
char grade = 'B';
System.out.println("grade: " + grade);
Predict both lines of output before reading on.
Reveal
x: 15
grade: B
x + 5 is computed as 15 and stored back into x. The char 'B' joined to a String with + becomes the text B.
A Common Mistake
Using double quotes for a single character is a frequent error: char letter = "A"; does not compile, because "A" is a String and a String does not fit in a char box. Single quotes make a char ('A'); double quotes make a String ("A"). When a compiler message mentions a char literal or a String literal, check the quote style first. (Source: BJP (Reges and Stepp), Ch 2; JLS 25, §3.10.4 and §3.10.5.)
Go Deeper (optional)
For the curious: a char is really a 16-bit number underneath. char c = 'A'; System.out.println((int) c); prints 65, the code point for A. And int stops near two billion because 32 binary digits can represent only so many values, the same place-value idea as base ten, with base two instead. (This connects to how the real number system is modeled in computing.)
Check Yourself
Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.
Check your understanding
Classify the literal 3.14 .
What happens when you compile char letter = “A”; ?
int x = 10; x = x + 5; System.out.println(x); What prints?
int count; then System.out.println(count); with no assignment between. What happens?
Which type holds only true or false?