📦 Java Variables and Data Types — Primitives vs Reference Types
Master Java variables and data types — the 8 primitives (int, long, double, boolean, char...), reference types, autoboxing, var, and type casting with examples and interview Q&A.
Java is statically typed — every variable has a declared type the compiler checks. There are two families: primitives and reference types.
🔢 The 8 primitive types
byte(8-bit),short(16-bit),int(32-bit),long(64-bit) — integersfloat(32-bit),double(64-bit) — decimalschar(16-bit Unicode) — a single characterboolean—true/false
Primitives hold the value directly and live on the stack (for locals).
🟣 Reference types
Objects, arrays, String, and everything else. A reference variable holds a pointer to an object on the heap, not the object itself.
🎁 Wrapper classes & autoboxing
Each primitive has a wrapper object: int → Integer, double → Double, etc. Autoboxing converts automatically: Integer x = 5; boxes the int. Collections only hold objects, so you need wrappers there.
🆕 The var keyword (Java 10+)
var name = "Sam"; // inferred String
var count = 42; // inferred int
var list = new ArrayList<String>();var only infers local variables — it's still statically typed, just less verbose.
⚠️ Casting
Widening (int → long) is automatic. Narrowing (double → int) needs an explicit cast and may lose data: int n = (int) 3.9; // 3.
💻 Code Examples
Primitives and their ranges
int age = 30;
long population = 8_000_000_000L; // note the L suffix
double price = 19.99;
char grade = 'A';
boolean active = true;
System.out.println(Integer.MAX_VALUE);2147483647
Autoboxing in a collection
List<Integer> nums = new ArrayList<>();
nums.add(5); // autoboxes int -> Integer
int first = nums.get(0); // unboxes Integer -> int
System.out.println(first);5
Narrowing cast loses precision
double d = 9.99;
int n = (int) d; // explicit narrowing
System.out.println(n);9
⚠️ Common Mistakes
- Forgetting the L suffix on long literals bigger than int range — `8000000000` won't compile, `8000000000L` will.
- Comparing Integer objects with == instead of .equals() — == compares references, not values (beware the -128..127 cache).
- Expecting int division to give a decimal — `5 / 2` is `2`; cast first: `5.0 / 2`.
- Using float for money — floating point can't represent decimals exactly; use BigDecimal for currency.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What are the 8 primitive types in Java?Beginner
Q2.What's the difference between a primitive and a reference type?Beginner
Q3.What is autoboxing and unboxing?Intermediate
Q4.Why can comparing two Integers with == be unreliable?Intermediate
Q5.Why use BigDecimal instead of double for money?Advanced
🧠 Quick Summary
- Java is statically typed: 8 primitives + reference types.
- Primitives hold values; references point to heap objects.
- Wrapper classes (Integer, Double) enable autoboxing for collections.
- var infers local variable types (still static).
- Compare wrappers with .equals(), not ==; use BigDecimal for money.