Primitives and Wrapper Classes
Primitive Types
Java has eight primitive types, divided into four categories:
| Category | Types |
|---|---|
| Integer | byte, short, int, long |
| Floating-point | float, double |
| Character | char |
| Boolean | boolean |
Size and Range
| Type | Size | Min | Max | Default |
|---|---|---|---|---|
byte |
8 bits | -128 | 127 | 0 |
short |
16 bits | -32,768 | 32,767 | 0 |
int |
32 bits | -2,147,483,648 | 2,147,483,647 | 0 |
long |
64 bits | -2⁶³ | 2⁶³ - 1 | 0L |
float |
32 bits | ~1.4e-45 | ~3.4e+38 | 0.0f |
double |
64 bits | ~4.9e-324 | ~1.8e+308 | 0.0 |
char |
16 bits | '' (0) | '' (65,535) | '' |
boolean |
— | — | — | false |
Literal Formats
javaint decimal = 1_000_000; // underscore for readability (Java 7+)
int hex = 0xFF; // hexadecimal
int binary = 0b1010_1010; // binary (Java 7+)
long bigNum = 123_456_789L; // L suffix required for long literals
double pi = 3.14_159;
float tax = 0.07f; // f suffix required for float literalsExam tip: An integer literal without a suffix is always an
int. A floating-point literal without a suffix is always adouble.
Wrapper Classes
Each primitive has a corresponding wrapper class in java.lang:
| Primitive | Wrapper | Cache range |
|---|---|---|
byte |
Byte |
-128 to 127 |
short |
Short |
-128 to 127 |
int |
Integer |
-128 to 127 |
long |
Long |
-128 to 127 |
float |
Float |
none |
double |
Double |
none |
char |
Character |
0 to 127 |
boolean |
Boolean |
true and false |
Autoboxing and Unboxing
The compiler automatically converts between primitives and wrappers:
javaInteger boxed = 42; // autoboxing: int → Integer
int unboxed = boxed; // unboxing: Integer → int
List<Integer> list = new ArrayList<>();
list.add(7); // autoboxing happens here
int value = list.get(0); // unboxing happens hereThe Integer Cache
Wrapper objects within the cache range are reused — == may return true:
javaInteger a = 127;
Integer b = 127;
System.out.println(a == b); // true (cached)
Integer x = 128;
Integer y = 128;
System.out.println(x == y); // false (new objects)
System.out.println(x.equals(y)); // true (always use equals!)Exam tip: Always use
.equals()to compare wrapper objects. The==operator compares references, not values (except within the cache range).
Useful Wrapper Methods
java// Parsing strings
int i = Integer.parseInt("42");
double d = Double.parseDouble("3.14");
long l = Long.parseLong("9876543210");
// Converting to different bases
String binary = Integer.toBinaryString(10); // "1010"
String hex = Integer.toHexString(255); // "ff"
String octal = Integer.toOctalString(8); // "10"
// Min / Max constants
int maxInt = Integer.MAX_VALUE; // 2147483647
int minInt = Integer.MIN_VALUE; // -2147483648
// Comparing
int result = Integer.compare(5, 10); // negative (5 < 10)NullPointerException with Unboxing
Unboxing a null wrapper throws NullPointerException at runtime:
javaInteger n = null;
int value = n; // NullPointerException!
// Common trap in conditional expressions:
Integer x = null;
int y = (x != null) ? x : 0; // safe — guard before unboxingKey Points for the Exam
- Primitive types are not objects — they live on the stack and have no methods.
- Wrapper classes are immutable — operations create new objects.
==on wrappers compares references;.equals()compares values.- Autoboxing/unboxing can throw
NullPointerExceptionif the wrapper isnull. Integercaches values from -128 to 127 by default.floatneeds thefsuffix;longneeds theLsuffix.