Primitive types
Java has eight built-in types that hold a raw value rather than a reference to an object: byte, short, int, long, float, double, char, and boolean.
Detailed Explanation
Everything else in Java is an object living on the heap, reached through a reference. Primitives are the exception. An int variable holds the number itself, not a pointer to a number. That single difference drives their performance, their defaults, and most of their surprises.
They exist because objects are expensive. A heap object carries a header of 12 to 16 bytes, needs an allocation, and needs the garbage collector to reclaim it later. A loop counter does not deserve that. Primitives cost exactly their width and live in a local variable slot on the thread's stack or inline inside an object, so arithmetic compiles to single CPU instructions.
The eight types are fixed in size by the language specification, not by the platform. This is a deliberate break from C, where int might be 16, 32, or 64 bits depending on the compiler. In Java an int is exactly 32 bits everywhere, which is part of what makes bytecode portable.
| Type | Width | Range | Default |
|---|---|---|---|
byte | 8 bits | −128 to 127 | 0 |
short | 16 bits | −32,768 to 32,767 | 0 |
int | 32 bits | about ±2.1 billion | 0 |
long | 64 bits | about ±9.2 quintillion | 0L |
float | 32 bits | ~7 decimal digits | 0.0f |
double | 64 bits | ~15 decimal digits | 0.0d |
char | 16 bits | 0 to 65,535, unsigned | '\u0000' |
boolean | JVM-dependent | true / false | false |
All integer types are signed except char. There is no unsigned int. Java 8 added static helpers such as Integer.toUnsignedLong and Long.divideUnsigned for when you must treat the bits as unsigned, typically when talking to a binary protocol.
Fields of primitive type get a default value automatically; local variables do not. A local must be assigned before use or the compiler rejects the code, which catches a whole class of bugs that languages with implicit zeroing let through.
Arithmetic has rules worth memorising. Anything narrower than int is promoted to int before the operation, so adding two byte values produces an int. Mixing an int and a long promotes to long. Integer division truncates toward zero, so 7 / 2 is 3. Overflow wraps silently rather than throwing, which is why Integer.MAX_VALUE + 1 is negative. Java 8 added Math.addExact and friends, which throw ArithmeticException instead of wrapping.
Reach for the widest sensible type by default: int for counts and indices, long for timestamps, identifiers, and anything counting money in minor units. Use byte and short only inside large arrays where the memory saving is real; a single short field usually occupies the same space as an int after alignment padding.
The cost of primitives is that they are not objects. They cannot be null, cannot be type arguments to generics before Project Valhalla lands, and cannot receive method calls. The moment you put numbers in a List, boxing happens and you pay the object cost anyway.
Code Examples
public class PrimitiveArithmetic {
public static void main(String[] args) {
int big = Integer.MAX_VALUE;
System.out.println(big + 1);
// prints: -2147483648 — silent wraparound, no exception
System.out.println(7 / 2); // prints: 3 integer division truncates
System.out.println(7 % -2); // prints: 1 remainder takes the sign of the left side
long millis = 1_000 * 60 * 60 * 24 * 30; // both operands are int, so this overflows
System.out.println(millis);
// prints: -1702967296 — widening to long happens AFTER the int multiply
long correct = 1_000L * 60 * 60 * 24 * 30; // the L forces long arithmetic
System.out.println(correct);
// prints: 2592000000
}
}public class FloatingPointMoney {
public static void main(String[] args) {
// double cannot represent 0.1 exactly, the same way decimal cannot represent 1/3.
System.out.println(0.1 + 0.2);
// prints: 0.30000000000000004
// Never store currency in double. Use minor units in a long, or BigDecimal.
long priceInCents = 1999; // 19.99
long taxInCents = 175;
System.out.println((priceInCents + taxInCents) / 100.0);
// prints: 21.74
// char is an unsigned 16-bit number, so it does arithmetic.
char letter = 'a';
System.out.println((char) (letter + 1)); // prints: b
}
}Common Gotchas
Integer overflow is silent
multiplying durations, byte counts, or IDs as `int` wraps around without any error. Use `long`, or `Math.multiplyExact` when you want an exception instead of a wrong answer.
Local variables have no default
`int total;` followed by `System.out.println(total);` fails to compile with "variable total might not have been initialized". Only fields and array elements are auto-zeroed.
`float` and `double` are binary approximations
`0.1 + 0.2 != 0.3`, and comparing them with `==` is unreliable. Use `BigDecimal` for money and a tolerance-based comparison for measurements.
Narrowing needs an explicit cast, and it truncates
`(byte) 200` gives `-56` because only the low 8 bits survive. The compiler forces the cast so the loss is at least visible in the source.