Beginner⏱️ 9 min📘 Topic 3 of 22

📦 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) — integers
  • float (32-bit), double (64-bit) — decimals
  • char (16-bit Unicode) — a single character
  • booleantrue / 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);
Output:
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);
Output:
5

Narrowing cast loses precision

double d = 9.99;
int n = (int) d;   // explicit narrowing
System.out.println(n);
Output:
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
byte, short, int, long (integers); float, double (decimals); char (Unicode character); boolean (true/false).
Q2.What's the difference between a primitive and a reference type?Beginner
A primitive stores the actual value. A reference type stores a pointer to an object on the heap. Primitives are compared by value with ==; references compare identity with == and content with .equals().
Q3.What is autoboxing and unboxing?Intermediate
Automatic conversion between a primitive and its wrapper class: int ↔ Integer, double ↔ Double. It lets primitives be used where objects are required (e.g., collections) but can cause subtle NullPointerExceptions when unboxing a null wrapper.
Q4.Why can comparing two Integers with == be unreliable?Intermediate
Integer caches values from -128 to 127, so == may be true for small numbers but false for larger ones (different objects). Always use .equals() to compare wrapper values.
Q5.Why use BigDecimal instead of double for money?Advanced
double is binary floating point and can't represent values like 0.1 exactly, causing rounding errors. BigDecimal stores decimal digits precisely and lets you control rounding — essential for financial calculations.

🧠 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.