Skip to content
Java fundamentals 4 min read

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.

TypeSizeRangeDefaultExample
byte8-bit-128 to 1270byte b = 100;
short16-bit-32,768 to 32,7670short s = 1000;
int32-bit-2³¹ to 2³¹-10int i = 100000;
long64-bit-2⁶³ to 2⁶³-10Llong l = 9_000_000_000L;
float32-bit~±3.4e38 (7 digits)0.0ffloat f = 3.14f;
double64-bit~±1.8e308 (15 digits)0.0ddouble d = 3.14159;
char16-bit to ￿ (0–65,535)char c = 'A';
booleanJVM-dependenttrue / falsefalseboolean 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: int and double are the default types for integer and decimal literals. Use the L and f suffixes when you need long or float.

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 null wrapper throws NullPointerException. Be careful with Integer 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 int and double by default; reach for long/float/byte only when range or memory demands it.
  • Never use float/double for money — use BigDecimal to 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.

Last updated June 1, 2026
Was this helpful?