Java variables have an explicit type declared before the name: int count = 0;. Unlike JavaScript or PHP, you cannot silently change a variable from int to String without a compile error.
Primitive types (common)
int,long— integersdouble,float— floating point (preferdouble)boolean—trueorfalseonlychar— single Unicode character in single quotes
Reference types
Objects and arrays are references: String name = "Ada";, int[] scores = {90, 85};. Primitives hold values directly; references point to heap objects.
final and var
final int MAX = 100; cannot be reassigned. Java 10+ var infers type locally: var list = new ArrayList<String>();
Important interview questions and answers
- Q: Primitive vs wrapper?
A:intis a primitive;Integeris an object wrapper used in generics and nullable contexts. - Q: Is String a primitive?
A: No—Stringis an immutable reference type. - Q: What is autoboxing?
A: Automatic conversion between primitives and wrappers, e.g.Integer n = 5;
Self-check
- Name two primitive numeric types.
- What type does
"hello"have?
Pitfall: Use equals for String content—== compares references and breaks comparisons of separate String objects.
Interview prep
- Primitive vs wrapper?
Primitives (
int) hold values directly; wrappers (Integer) are objects used in generics and nullable contexts—with autoboxing between them.