Variables and Data Types · Lesson 5 of 42
Data Types: Primitives, Wrappers and String
Java has two families of types. Primitive types hold simple values directly – numbers, characters, true/false. Reference types – classes such as String, arrays and your own objects – hold a reference (an address) to an object elsewhere in memory. Knowing which is which explains a surprising number of interview questions.
In this lesson
- The eight primitive types, their sizes and ranges
- Integer overflow and floating-point precision
- Primitives vs references in memory
- Wrapper classes, autoboxing and the
Integercache trap
The eight primitives
| Type | Size | Range / values | Example |
|---|---|---|---|
byte | 8 bits | −128 to 127 | byte b = 100; |
short | 16 bits | −32,768 to 32,767 | short s = 3000; |
int | 32 bits | about ±2.1 billion | int marks = 92; |
long | 64 bits | about ±9.2 × 10¹⁸ | long views = 9_000_000_000L; |
float | 32 bits | ~7 decimal digits | float f = 3.14f; |
double | 64 bits | ~15 decimal digits | double pi = 3.14159; |
char | 16 bits | one Unicode character | char grade = 'A'; |
boolean | – | true / false | boolean ok = true; |
Defaults to remember: use int for whole numbers, long when values can exceed 2 billion (IDs, timestamps, bank balances in paise), and double for decimals. Note the L on long literals and the f on float literals.
public class Main {
public static void main(String[] args) {
System.out.println("int: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
System.out.println("long: " + Long.MAX_VALUE);
System.out.println("char: " + (int) Character.MAX_VALUE);
System.out.println("byte: " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
}
}
int: -2147483648 to 2147483647 long: 9223372036854775807 char: 65535 byte: -128 to 127
Overflow: when numbers wrap around
Integer types don't raise an error when they overflow – they silently wrap to the other end of the range. This has caused real production bugs.
public class Main {
public static void main(String[] args) {
int max = Integer.MAX_VALUE;
System.out.println(max + 1); // wraps around!
long safe = (long) max + 1;
System.out.println(safe);
int secondsInYear = 365 * 24 * 60 * 60;
System.out.println(secondsInYear * 100); // overflow again
System.out.println(secondsInYear * 100L); // long arithmetic
}
}
-2147483648 2147483648 -1141367296 3153600000
Tip
Use Math.addExact(a, b) or Math.multiplyExact(a, b) when overflow must never happen – they throw ArithmeticException instead of wrapping. For money, use long paise or BigDecimal, never double.
Floating-point precision
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
System.out.println(0.1 + 0.2);
System.out.println(0.1 + 0.2 == 0.3);
System.out.println(new BigDecimal("0.1").add(new BigDecimal("0.2")));
System.out.println(10 / 4); // int division
System.out.println(10 / 4.0); // double division
}
}
0.30000000000000004 false 0.3 2 2.5
char is a number too
A char stores a 16-bit Unicode value, so you can do arithmetic with it:
public class Main {
public static void main(String[] args) {
char c = 'A';
System.out.println((int) c); // its code
System.out.println((char) (c + 2)); // C
System.out.println(c + 1); // char + int → int
char rupee = '₹';
System.out.println(rupee);
}
}
65 C 66 ₹
Primitives vs references
Where the value lives
Variable
- Primitive: int marks = 92the value 92 is stored right in the variable (on the stack for locals)
- Reference: String name = "Asha"the variable holds an address; the String object lives on the heap
Two practical consequences: == on references compares addresses, not contents (use .equals() for strings), and a reference can be null – pointing at nothing – while a primitive can't.
Wrapper classes and autoboxing
Each primitive has an object version: Integer, Long, Double, Character, Boolean and so on. Collections such as ArrayList can only hold objects, so Java converts automatically – autoboxing (int → Integer) and unboxing (Integer → int).
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>();
marks.add(85); // autoboxing
int first = marks.get(0); // unboxing
System.out.println(first + 5);
System.out.println(Integer.parseInt("120") + 1);
System.out.println(Integer.toBinaryString(10));
System.out.println(Character.isDigit('7') + " " + Character.toUpperCase('j'));
}
}
90 121 1010 true J
The Integer cache trap
public class Main {
public static void main(String[] args) {
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b); // same cached object
System.out.println(c == d); // two different objects
System.out.println(c.equals(d)); // compare values
}
}
true false true
Common mistake: comparing wrappers with ==
Java caches Integer objects from −128 to 127, so == happens to work for small numbers and fails for larger ones. Always compare wrapper objects with .equals(). Also beware: unboxing a null Integer throws NullPointerException.
String in brief
String is a class, not a primitive, but Java gives it special treatment: literals in double quotes, + for joining, and a pool of reused literals. Strings are immutable – every "change" creates a new string. Chapter 6 covers strings in depth.
Practice
Try it yourself
Calculate the number of milliseconds in 30 days without overflow.
Show solution
public class Main { public static void main(String[] args) { long ms = 30L * 24 * 60 * 60 * 1000; System.out.println(ms); } }2592000000
Print the next three letters after 'x' using char arithmetic.
Show solution
public class Main { public static void main(String[] args) { char c = 'x'; for (int i = 1; i <= 3; i++) { System.out.print((char) (c + i) + " "); } } }y z {Notice the third character is
{– the next Unicode value afterz, nota. Wrapping around the alphabet needs%.
Quick check
How many primitive types does Java have?
Eight: byte, short, int, long, float, double, char, boolean.
What is Integer.MAX_VALUE + 1?
Integer.MIN_VALUE (−2147483648) – integer overflow wraps around.
Why can't you write List<int>?
Generics work with objects only; use the wrapper type List<Integer>.
Summary
- 8 primitives store values directly; everything else is a reference to an object.
- Integers overflow silently; floats are approximate – use
long/BigDecimalfor money. - Wrappers allow primitives in collections; compare them with
.equals(), not==.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.