Nearby lessons
50 of 125Java - Number, Boolean and Character
📌 What You Will Learn
- Why wrapper classes exist
- How to convert primitive to object and object to primitive
- Autoboxing and unboxing (automatic conversion)
- Important methods: valueOf, parseXxx, toString, xxxValue
- The famous Integer caching (range -128 to 127)
Number, Boolean and Character is a core concept of the Java language. This lesson explains Important Methods of Wrapper Classes with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Important Methods of Wrapper Classes
Wrapper classes give many useful static methods. Memorise these five families:
| Method | Purpose | Example |
|---|---|---|
| valueOf(String) | Convert a String into a wrapper object | Integer.valueOf("100") |
| parseXxx(String) | Convert a String into a primitive | Integer.parseInt("100") -> int 100 |
| toString(xxx) | Convert a primitive into String | Integer.toString(100) -> "100" |
| xxxValue() | Convert wrapper to primitive | i.intValue(), d.doubleValue() |
| MIN_VALUE / MAX_VALUE | Range constants of each type | Integer.MAX_VALUE |
Trainer's Note: Classic interview question: what is the difference between parseInt and valueOf? parseInt("100") returns an int (primitive). valueOf("100") returns an Integer (object). One gives the gift, the other gives the gift in a box.
In simple words: `parseInt` returns a primitive `int`; `valueOf` returns an `Integer` object. Use parseInt when you need a number to calculate with, and valueOf when you need an object for a collection.
Example01
📝 Key Takeaways
- Wrapper classes box each primitive into an object: int->Integer, char->Character, etc.
- Autoboxing converts primitive to wrapper automatically; unboxing does the reverse.
- parseInt returns primitive int; valueOf returns Integer object.
- Use equals() not == for wrapper objects and Strings.
- Integer values from -128 to 127 are cached, so == can be true for small values.
- Collections can store only objects, so wrappers are essential there.
🧠 Test Your Knowledge
2 QuestionsProgress: 0 / 2