What are Java Data Types?
Learn Java's eight primitive types, reference types, and wrapper classes, with examples of autoboxing, default values, and stack vs heap storage.
Expected Interview Answer
Java data types split into eight primitive types (byte, short, int, long, float, double, char, boolean) that store raw values directly, and reference types (classes, interfaces, arrays) that store a reference pointing to an object on the heap.
Primitives have fixed sizes and default values, live on the stack when local, and are never null. Reference types can be null, are allocated on the heap, and are accessed through a variable holding a memory reference rather than the object itself. Java also provides wrapper classes (Integer, Double, Boolean, and so on) that box primitives into objects so they can be used in generics and collections, with autoboxing/unboxing converting between the two automatically. Choosing the right type affects memory footprint, performance, and null-safety.
- Primitives are fast and memory-efficient with no object overhead
- Reference types enable rich behavior via objects and collections
- Wrapper classes bridge primitives into the generics/collections world
- Fixed primitive sizes make behavior predictable across platforms
- Clear typing catches many errors at compile time
AI Mentor Explanation
Primitive types are like the raw runs scored, a simple fixed number stored directly on the scoreboard. Reference types are like a player's full profile card kept in the pavilion, where the scoreboard only holds a pointer such as a shirt number to look up when you need the detail.
Step-by-Step Explanation
Step 1
Learn the eight primitives
byte, short, int, long, float, double, char, boolean each have a fixed size and a default value.
Step 2
Understand reference types
Classes, interfaces, and arrays are reference types; the variable holds a pointer to a heap-allocated object.
Step 3
Know where each lives
Local primitives typically sit on the stack; objects always live on the heap, referenced by a stack variable.
Step 4
Use wrapper classes when needed
Integer, Double, Boolean, etc. box a primitive into an object for use in generics and collections.
Step 5
Rely on autoboxing/unboxing carefully
Java automatically converts between primitives and wrappers, but watch for NullPointerException when unboxing a null wrapper.
What Interviewer Expects
- Can name all eight primitive types
- Explains the difference between primitive and reference types clearly
- Knows wrapper classes exist and why they're needed
- Understands autoboxing/unboxing and its pitfalls
- Mentions default values and stack vs heap at a high level
Common Mistakes
- Forgetting char or boolean when listing primitives
- Saying String is a primitive type (it is a reference type)
- Thinking primitives can be null
- Assuming autoboxing is free with no performance or NPE cost
- Confusing stack/heap allocation with garbage collection eligibility
Best Answer (HR Friendly)
“Java has simple built-in types like numbers, characters, and true/false values called primitives, plus more complex types like objects, arrays, and strings that are called reference types. Java also has wrapper versions of primitives so they can be used anywhere an object is required, such as in lists.”
Code Example
int age = 30; // primitive, stores value directly
String name = "Ada"; // reference type, points to a heap object
Integer boxedAge = age; // autoboxing: int -> Integer
int unboxedAge = boxedAge; // auto-unboxing: Integer -> int
Integer maybeNull = null;
try {
int crash = maybeNull; // NullPointerException on unboxing
} catch (NullPointerException e) {
System.out.println("Unboxing null throws NPE");
}Follow-up Questions
- What is the difference between int and Integer?
- Why is String considered a reference type but treated specially?
- What are the default values of each primitive type?
- What is autoboxing and when can it cause a NullPointerException?
- How does Java handle numeric overflow for int and long?
MCQ Practice
1. Which of the following is NOT a Java primitive type?
String is a reference type (a class), not one of the eight primitives.
2. Where does a local primitive variable typically get allocated?
Local primitive variables are typically stored on the stack frame of the executing method.
3. What happens when you unbox a null Integer to an int?
Auto-unboxing calls intValue() on the wrapper, and calling a method on null throws NullPointerException.
Flash Cards
Name Java's eight primitive types. — byte, short, int, long, float, double, char, boolean.
What's the key difference between primitive and reference types? — Primitives store the value directly; reference types store a pointer to a heap object and can be null.
What is a wrapper class? — An object wrapper around a primitive (e.g. Integer for int) so it can be used in generics/collections.
What risk does autoboxing introduce? — Unboxing a null wrapper reference throws a NullPointerException.