📦 Variables & Types · Beginner

Wrapper classes in Java

Integer, Double, Boolean…, parsing (Integer.parseInt) and valueOf.

🧩 The mysteryA Java list can't hold a plain 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 -> Boolean

Text 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");
🔮 Predict it

Your turn

What does this print?

int n = Integer.parseInt("20");
System.out.println(n + 1);
  1. 201
  2. 21
  3. Compile error
Show the answer

parseInt gives a real int 20, so + 1 adds: 21. ("20" + 1 would have joined text and given "201".)

⚠️ The trap

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.5

Other 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"
🔮 Predict it

Your turn

What does this print?

System.out.println(Integer.parseInt("11", 2));
System.out.println(Integer.toBinaryString(6));
  1. 3 110
  2. 11 6
  3. 3 6
Show the answer

"11" in base 2 is 3. And 6 in binary is 110, returned as text.

💼 In the real world

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

  1. int ↔ Integer, char ↔ Character, boolean ↔ Boolean
  2. Integer.parseInt("42") → int 42
  3. Integer.valueOf("42") → Integer object
  4. Bad text → NumberFormatException
🤯 Did you know?

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)?

  1. parseInt returns an Integer object; valueOf returns an int primitive
  2. parseInt returns an int primitive; valueOf returns an Integer object
  3. parseInt accepts decimals; valueOf accepts only whole numbers
  4. 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");
  1. Throws NumberFormatException
  2. 4
  3. 5
  4. 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.

Next: Java converts between int and Integer automatically. Convenient, until a null sneaks in and crashes your program.