UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.2
1.2 Variables and Data Types
Java is statically typed: every variable is declared with a type that never changes. The exam's primitive types are int, double, and boolean; everything else is a reference.
What you need to know
- A variable is a named memory location with a declared type. Declaration:
int count;. Declaration with initialization:double price = 9.99; - Primitive types on the exam:
int(whole numbers),double(decimals),boolean(true/false). A primitive variable stores the value itself. - Reference types (like
Stringand every other class) store the address of an object, not the object. Two reference variables can point to the same object. inthas a fixed range:Integer.MIN_VALUE(−2,147,483,648) toInteger.MAX_VALUE(2,147,483,647). Exceeding it causes overflow — it wraps around silently, no error.- The
finalkeyword makes a variable a constant:final int MAX = 100;. Assigning to it again is a compile-time error. - Naming: variables start with a lowercase letter (
totalScore); constants are all caps (MAX_SIZE); classes start uppercase (String). Names must be declared before use and are case-sensitive.
Worked example
int age = 17; double gpa = 3.85; boolean isSenior = true; String name = "Ram"; final double TAX = 0.07; age = 18; // fine TAX = 0.08; // compile-time error: cannot assign to final
Four types, four kinds of value. name holds a reference to a String object; the other three hold their values directly. That distinction matters in 1.13 and 3.6.
Exam tip: Questions on ranges are usually about overflow:
Integer.MAX_VALUE + 1 equals Integer.MIN_VALUE, not an error. And a variable declared but never assigned can't be used — that's a compile error, not a default of 0.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- The exam's three primitives are
int,double, andboolean. Java has others (char,long,float…) but the CED excludes them. If you seecharon the exam, it's in a String context and the question will explain. - int is a 32-bit signed integer: exactly −2,147,483,648 to 2,147,483,647.
Integer.MAX_VALUEandInteger.MIN_VALUEare on the reference sheet. Overflow wraps silently: MAX + 1 = MIN. No exception. - double holds decimals with about 15–16 significant digits of precision. Not every decimal is exact —
0.1 + 0.2is0.30000000000000004. This is why comparing doubles with==is unreliable. - A variable must be declared before use and initialized before its value is read. A declared-but-unassigned local variable is a compile error when read. (Instance variables are different — they get defaults. That's 3.3.)
- Naming rules: start with a letter,
_, or$; no spaces; case-sensitive; can't be a keyword. Conventions: camelCase for variables and methods, PascalCase for classes, ALL_CAPS for constants. Conventions aren't enforced by the compiler but the exam follows them and expects you to. finalvariables must be initialized exactly once. Attempting a second assignment is a compile error. Constants make code readable (MAX_SPEEDinstead of120) and safe from accidental change.
Mistakes that cost points
- Assigning a double to an int.
int x = 3.7;won't compile. Needs a cast. The reverse (double d = 3;) is fine. - Expecting overflow to throw. It wraps.
Integer.MAX_VALUE + 1is a large negative number. - Assuming a local variable defaults to 0. It has no default. Reading it before assignment is a compile error.
- Using a keyword as a name.
int class = 5;won't compile.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following declarations will cause a compile-time error?
Show answer
Answer: C. A double literal can't be assigned to an int without a cast. Assigning an int to a double (option B) is fine — it widens automatically.
Q2 What is the result of evaluating
Integer.MAX_VALUE + 1?Show answer
Answer: C. Integer overflow wraps around silently to the minimum value. Java doesn't throw an error.
Key vocabulary
- Primitive type
- int, double, boolean — stores the value directly
- Reference type
- a variable that stores the address of an object
- final
- keyword that makes a variable's value unchangeable after initialization
- Overflow
- an int calculation exceeding the representable range wraps around