Wrapper classes in Java
Integer, Double, Boolean…, parsing (Integer.parseInt) and valueOf.
int. So how do you store numbers in a list? Meet the wrappers: primitives in object costumes.Objects that wrap a primitive
Each primitive has a wrapper class that represents it as an object. Most are just the capitalized name; int and char are the odd ones out. Wrappers are needed wherever objects are required, like in collections.
byte -> Byte short -> Short
int -> Integer long -> Long
float -> Float double -> Double
char -> Character
boolean -> BooleanText to numbers
Integer.parseInt("42") returns a primitive int. Integer.valueOf("42") returns an Integer object (and may reuse a cached one). Same parsing, different result type. Wrappers also hold constants like Integer.MAX_VALUE.
int n = Integer.parseInt("42");
Integer obj = Integer.valueOf("42");Your turn
What does this print?
int n = Integer.parseInt("20");
System.out.println(n + 1);20121Compile error
Show the answer
parseInt gives a real int 20, so + 1 adds: 21. ("20" + 1 would have joined text and given "201".)
Bad text explodes at runtime
parseInt accepts only an optional sign followed by digits. A decimal point, a space or a letter throws NumberFormatException at runtime. It won't drop the .5 for you. For decimals, use Double.parseDouble.
Integer.parseInt("4.5") // exception
Integer.parseInt(" 7") // exception
Double.parseDouble("4.5") // 4.5Other number bases
parseInt takes an optional second argument, the radix (base). Integer.parseInt("101", 2) reads binary and returns 5. Integer.toBinaryString(5) goes the other way and returns the text "101".
Integer.parseInt("101", 2) // 5
Integer.parseInt("ff", 16) // 255
Integer.toBinaryString(5) // "101"Your turn
What does this print?
System.out.println(Integer.parseInt("11", 2));
System.out.println(Integer.toBinaryString(6));3 11011 63 6
Show the answer
"11" in base 2 is 3. And 6 in binary is 110, returned as text.
In real projects
Every number from a web form, file, URL or API arrives as text. Real code catches NumberFormatException: a user typing "12abc" into an age field must get a friendly message, not crash your server.
Key takeaways
- int ↔ Integer, char ↔ Character, boolean ↔ Boolean
- Integer.parseInt("42") → int 42
- Integer.valueOf("42") → Integer object
- Bad text → NumberFormatException
new Integer(5) has been deprecated since Java 9 and marked for removal since Java 16. Use Integer.valueOf(5), or just let autoboxing do it.
Practice questions
What's the difference between Integer.parseInt(s) and Integer.valueOf(s)?
- parseInt returns an Integer object; valueOf returns an int primitive
- parseInt returns an int primitive; valueOf returns an Integer object
- parseInt accepts decimals; valueOf accepts only whole numbers
- There is no difference; they're aliases
Check your answer
parseInt returns an int primitive; valueOf returns an Integer object. Both parse the same text. Use parseInt when you need a number, valueOf when you need an object (it can also reuse cached Integers).
What does this print?
int n = Integer.parseInt("4.5");- Throws NumberFormatException
- 4
- 5
- Compile error
Check your answer
Throws NumberFormatException. parseInt accepts only optional sign + digits. "4.5" contains a decimal point, so it throws NumberFormatException at runtime. Use Double.parseDouble for decimals.