Ahmad was a businessman. He travelled to the United States, found an amazing deal at a factory, and bought one million electronic devices — investing everything he had. The shipment arrived at the port of Jeddah. He opened the first box, plugged a device into the wall… nothing. A burning smell. 💨
The devices were built for 110V. Saudi Arabia runs on 220V. One million devices — completely incompatible. He could not sell a single one. His business was over before it started.
Then his friend Khalid called. "Ahmad — do not touch a single device. Build a small box: it takes 220V from the wall, converts it to 110V inside, and gives your device exactly what it needs. Place it in between." Ahmad placed the wrapper box in between — nothing changed on either side — and his business was saved. 🎉
Java has the same problem. Primitives (int, double, char…) work perfectly on their own, but Java's object-oriented world — ArrayList, generics, APIs — only accepts objects. Java's solution is exactly Khalid's box: a Wrapper class (Integer, Double, Character…) placed in between. Nothing on either side is modified.
In Java, there are 8 primitive data types: byte, short, int, long, float, double, char, boolean. Some Java APIs and data structures (like ArrayList) can only work with objects, not primitives. Solution? Java provides a wrapper class for each primitive that wraps it in an object!
intValue(), compareTo(), parseInt()…
| Primitive Type | Wrapper Class | Example |
|---|---|---|
byte | Byte | Byte b = new Byte((byte)5); |
short | Short | Short s = new Short((short)100); |
int | Integer | Integer i = new Integer(42); |
long | Long | Long l = new Long(100L); |
float | Float | Float f = new Float(3.14f); |
double | Double | Double d = new Double(2.718); |
char | Character | Character c = new Character('A'); |
boolean | Boolean | Boolean b = new Boolean(true); |
Java's wrapper classes are built-in — but you can also build your own wrapper class. This is a powerful idea: take any primitive, wrap it inside a class, and add whatever methods you need. For example, we can define a class called MyInt that wraps an int and provides extra functionality.
Some Java APIs (like ArrayList<Integer>, HashMap<String, Double>) only accept objects (they use generics). You cannot write ArrayList<int>. So wrapper classes let you store primitive values inside these structures.
Java's auto-boxing and auto-unboxing features (since Java 5) handle most conversions automatically, so you rarely need to call .intValue() manually.
- Auto-boxing: Java automatically converts a primitive to its wrapper type when needed.
Integer x = 5;→ Java writesnew Integer(5)for you. - Auto-unboxing: Java automatically converts a wrapper object back to its primitive.
int y = x;→ Java callsx.intValue()for you. - Useful constants:
Integer.MAX_VALUE,Integer.MIN_VALUE,Double.MAX_VALUE. - Parsing:
Integer.parseInt("42")converts aStringto anint— very common in real programs!
What if the number you need to compute is so large that it doesn't fit in a long (which maxes out at about 9.2 × 1018)? Or you need extremely precise decimal arithmetic — like in financial calculations or cryptography?
Java provides two special classes: BigInteger and BigDecimal in the java.math package.
add(), subtract(), multiply(), divide(), pow(), mod().You cannot use +, -, *, / operators with BigInteger or BigDecimal. You must use their methods: .add(), .subtract(), .multiply(), .divide().
A student asked: "Why would we ever need numbers this large?"
In cryptography, algorithms like RSA require working with integers that are hundreds of digits long (e.g., 2048-bit or 4096-bit numbers). The prime factorisation of such numbers is what makes encryption secure. BigInteger is the standard tool for implementing these algorithms in Java.
BigInteger and BigDecimal are not strictly OOP concepts — they are Java library features. We discuss them here because they demonstrate how Java wraps complex numeric behaviour inside classes with methods, which is a great example of encapsulation and abstraction in action.
new. UML: filled diamond ◆.Student supervisor inside Student). Foundation for Linked Lists in Data Structures.1, 0..1, *, 1..*, ranges like 5..60.int → Integer) to allow them to be used as objects. Auto-boxing/unboxing handles conversions automatically.add(), multiply()) — not operators. Essential for cryptography and finance.