Sub topic 1
Primitives and Objects: Why Java Has Both
Prerequisites
You should know Java’s primitive types, basic class and object syntax, and the difference between == and equals for objects. You should also have used generic collections such as List<Integer>.
Java has two broad categories of values:
- A primitive is a directly stored value, such as an
int,double,boolean, orchar. - A reference is a value that points to an object. The object stores data and provides methods.
For example, int is primitive, while Integer is a reference type. Both can represent whole numbers, but they behave differently.
| Property | `int` | `Integer` |
|---|---|---|
| Category | Primitive | Reference type |
| Can be `null` | No | Yes |
| Has methods | No | Yes |
| Works as a generic type argument | No | Yes |
| Typical use | Efficient calculations | Objects, collections, optional values |
Primitive Values
A primitive variable holds a value directly:
int count = 42;
double price = 19.95;
boolean enabled = true;Primitives are designed for simple, efficient operations. An int has no methods, so you cannot write count.toString(). Instead, Java provides utility methods through classes such as Integer.
A primitive also always has a value after initialization. It cannot represent “missing” with null:
int count = 0;
// int missing = null; // Does not compileThe value 0 might mean “missing” in some program, but Java does not assign that meaning automatically.
Reference Values and Wrapper Classes
A reference variable points to an object:
Integer count = 42;
String name = "Mira";Integer is a wrapper class, which is a class that represents a primitive value as an object. Java provides one main wrapper for each primitive type:
| Primitive | Wrapper |
|---|---|
| `byte` | `Byte` |
| `short` | `Short` |
| `int` | `Integer` |
| `long` | `Long` |
| `float` | `Float` |
| `double` | `Double` |
| `char` | `Character` |
| `boolean` | `Boolean` |
The wrapper classes matter because many Java APIs work with objects rather than primitives. Generic type arguments must be reference types, so this is invalid:
// List<int> values; // Does not compileUse the wrapper instead:
import java.util.ArrayList;
import java.util.List;
public class PrimitiveObjectExample {
public static void main(String[] args) {
List<Integer> values = new ArrayList<>();
values.add(10);
values.add(20);
System.out.println(values.get(0));
}
}The list stores Integer objects, not int values. Java makes the conversion between int and Integer convenient.
Why Both Categories Exist
Primitives provide efficient storage and operations. They do not carry object identity, methods, or a null state.
Objects support features that primitives do not:
- They can be passed through APIs requiring references.
- They can represent the absence of a value with
null. - They provide methods such as
Integer.compareandInteger.parseInt. - They can participate in generic classes and collections.
This difference creates an important boundary in Java programs:
A reference can also be null, which means it points to no object:
Integer score = null;
// int result = score; // Fails at runtime during unboxingThis is one reason wrapper types require more careful handling than primitives. A primitive variable cannot be null, but a wrapper variable can.
The compiler can often convert between the two categories automatically. For example, assigning an int to an Integer boxes the value, while assigning an Integer to an int unboxes it. The next chapters examine those conversions and their failure cases.
Exercise
Why does List<Integer> compile while List<int> does not, and what risk does Integer introduce?
Show answer
Generic type arguments must be reference types, so Integer is used instead of int. An Integer can be null, so using it where an int is required can cause a NullPointerException.