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

TypeSizeRangeDefaultWrapper
byte8 bitsβˆ’128 to 1270Byte
short16 bitsβˆ’32 768 to 32 7670Short
int32 bitsβˆ’2Β³ΒΉ to 2Β³ΒΉβˆ’10Integer
long64 bitsβˆ’2⁢³ to 2βΆΒ³βˆ’10LLong
float32 bits~Β±3.4Γ—10³⁸ (7 digits)0.0fFloat
double64 bits~Β±1.8Γ—10³⁰⁸ (15 digits)0.0Double
booleanJVM-definedtrue / falsefalseBoolean
char16 bitsUTF-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 integer
  • long β€” when 32 bits aren't enough
  • double β€” IEEE-754 64-bit floats
  • boolean β€” true and false (and only those)
  • char β€” UTF-16 code units, not characters
  • byte β€” for binary data and arrays
  • short β€” rarely needed today
  • float β€” when memory matters

Common mistakes

  • Comparing wrappers with == β€” works for βˆ’128..127 by accident, then breaks. Use .equals().
  • Forgetting the L on long literals β€” 1024 * 1024 * 1024 * 4 overflows silently to a negative int.
  • Using float/double for money β€” switch to BigDecimal.
  • Treating char as a Unicode character β€” emoji and supplementary characters need two chars. Use String and codePointAt().

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.