Java Primitives β int, double, boolean and the Eight Built-in Types
Java has exactly eight primitive types. They aren't objects β they hold a raw value directly in memory, with no header and no methods. Every other type in Java (String, arrays, your own classes) is a reference type.
The eight primitives at a glance
| Type | Size | Range | Default | Wrapper |
|---|---|---|---|---|
byte | 8 bits | β128 to 127 | 0 | Byte |
short | 16 bits | β32 768 to 32 767 | 0 | Short |
int | 32 bits | β2Β³ΒΉ to 2Β³ΒΉβ1 | 0 | Integer |
long | 64 bits | β2βΆΒ³ to 2βΆΒ³β1 | 0L | Long |
float | 32 bits | ~Β±3.4Γ10Β³βΈ (7 digits) | 0.0f | Float |
double | 64 bits | ~Β±1.8Γ10Β³β°βΈ (15 digits) | 0.0 | Double |
boolean | JVM-defined | true / false | false | Boolean |
char | 16 bits | UTF-16 code unit | '\u0000' | Character |
Defaults only apply to fields
Instance and static fields are initialised to the default value above. Local variables are not β Java rejects code that reads an uninitialised local:
int x;
System.out.println(x); // β "variable x might not have been initialized"
Literals: the suffixes that bite
long big = 3_000_000_000; // β int overflow before assignment
long big2 = 3_000_000_000L; // β
L suffix forces long literal
float pi = 3.14; // β double cannot be assigned to float
float pi2 = 3.14f; // β
f suffix forces float literal
int hex = 0xFF; // 255
int binary = 0b1010_1010; // 170 β underscores allowed since Java 7
Floating-point precision
float and double are binary floating-point (IEEE 754). Decimal numbers like 0.1 can't be represented exactly:
System.out.println(0.1 + 0.2); // 0.30000000000000004
For money or anything that must round predictably, use BigDecimal with a string constructor: new BigDecimal("0.1").
Wrappers and autoboxing
Each primitive has a wrapper class (Integer, Double, β¦). Java auto-boxes between them, but two traps:
Integer a = 127, b = 127;
Integer c = 200, d = 200;
System.out.println(a == b); // true β cached for β128..127
System.out.println(c == d); // false β different objects
System.out.println(c.equals(d)); // true β
always use .equals()
Integer x = null;
int y = x; // β NullPointerException on auto-unboxing
Modern Java: var still infers primitives
var (Java 10+) infers the type from the initialiser:
var i = 42; // int
var d = 3.14; // double
var b = true; // boolean
var s = "hello"; // String β reference, not primitive
All sub-topics
intβ the workhorse 32-bit integerlongβ when 32 bits aren't enoughdoubleβ IEEE-754 64-bit floatsbooleanβ true and false (and only those)charβ UTF-16 code units, not charactersbyteβ for binary data and arraysshortβ rarely needed todayfloatβ when memory matters
Common mistakes
- Comparing wrappers with
==β works for β128..127 by accident, then breaks. Use.equals(). - Forgetting the
Lon long literals β1024 * 1024 * 1024 * 4overflows silently to a negativeint. - Using
float/doublefor money β switch toBigDecimal. - Treating
charas a Unicode character β emoji and supplementary characters need twochars. UseStringandcodePointAt().
Try it & related tools
Run any snippet in the Java Online Compiler. To inspect numeric edge cases, try the Base64 tool for byte-level work or the String Escape tool for char and \uXXXX handling.