Data Types
Java is a statically and strongly typed language: every variable and expression has a type known at compile time. Types fall into two families — primitive types, which hold raw values directly, and reference types, which hold references to objects on the heap.
The Eight Primitive Types
Primitives are not objects; they store their value directly and have fixed sizes independent of platform.
| Type | Size | Range | Default | Example |
|---|---|---|---|---|
byte | 8-bit | -128 to 127 | 0 | byte b = 100; |
short | 16-bit | -32,768 to 32,767 | 0 | short s = 1000; |
int | 32-bit | -2³¹ to 2³¹-1 | 0 | int i = 100000; |
long | 64-bit | -2⁶³ to 2⁶³-1 | 0L | long l = 9_000_000_000L; |
float | 32-bit | ~±3.4e38 (7 digits) | 0.0f | float f = 3.14f; |
double | 64-bit | ~±1.8e308 (15 digits) | 0.0d | double d = 3.14159; |
char | 16-bit | � to (0–65,535) | � | char c = 'A'; |
boolean | JVM-dependent | true / false | false | boolean ok = true; |
long big = 9_000_000_000L; // underscores aid readability; L required
float pi = 3.14f; // f suffix required for float literals
char grade = 'A'; // single quotes for char
int hex = 0xFF; // 255 in hexadecimal
int bin = 0b1010; // 10 in binary
Note:
intanddoubleare the default types for integer and decimal literals. Use theLandfsuffixes when you needlongorfloat.
Reference Types
Anything that is not a primitive is a reference type: classes, interfaces, arrays, enums, and records. A reference variable stores the address of an object, or null if it points to nothing.
String text = "DevCraftly"; // String object
int[] scores = {90, 85, 100}; // array object
Integer boxed = 42; // wrapper object (autoboxed)
String empty = null; // points to no object
Each primitive has a corresponding wrapper class (Integer, Double, Boolean, Character, …). Java performs autoboxing and unboxing to convert between them automatically.
Integer wrapped = 5; // autoboxing: int -> Integer
int back = wrapped; // unboxing: Integer -> int
Warning: Unboxing a
nullwrapper throwsNullPointerException. Be careful withInteger x = map.get(key); int y = x;when the key may be absent.
Type Casting
Conversion between numeric types is either widening (implicit, safe) or narrowing (explicit, may lose data).
// Widening — automatic, no precision loss
int i = 100;
long l = i; // int -> long
double d = l; // long -> double
// Narrowing — explicit cast required, may truncate
double pi = 3.99;
int truncated = (int) pi; // 3, decimal part dropped
long big = 300L;
byte b = (byte) big; // 44, overflow wraps around
System.out.println(truncated + " " + b);
Output:
3 44
The widening order is: byte → short → int → long → float → double. Anything going the other direction needs an explicit cast.
Local Variable Type Inference (var)
From Java 10, var infers a local variable’s type from its initializer. The type is still fixed and checked at compile time.
var count = 10; // int
var name = "Ada"; // String
var list = new ArrayList<String>(); // ArrayList<String>
var cannot be used without an initializer, for fields, parameters, or with null alone (the type would be unknowable).
Best Practices
- Use
intanddoubleby default; reach forlong/float/byteonly when range or memory demands it. - Never use
float/doublefor money — useBigDecimalto avoid rounding errors. - Prefer primitives over wrappers in hot paths to avoid boxing overhead.
- Make narrowing casts explicit and intentional; they signal possible data loss.
Interview Questions
Q: Why is 0.1 + 0.2 != 0.3 in Java?
A: double uses IEEE-754 binary floating point, which cannot represent decimal fractions like 0.1 exactly. The tiny representation errors accumulate. Use BigDecimal for exact decimal arithmetic.
Q: What is the difference between int and Integer?
A: int is a primitive holding a 32-bit value directly; Integer is a wrapper object on the heap that can be null and used in generics/collections. Conversion happens via autoboxing/unboxing.
Q: What happens during narrowing conversion overflow?
A: The high-order bits are discarded. For example, casting 300 to byte keeps only the low 8 bits, yielding 44. No exception is thrown, so it can silently corrupt data.