Arrays and Strings · Introduction to Java Programming

Integer Class

Watch lectures

Integer Class

1. Why Integer Is #1 Wrapper (beginner)

Integer wraps int (the default whole-number type!). Most-used wrapper: CLI args parsing, ArrayList<Integer>, string↔number exams. Master this one deeply!

int n = Integer.parseInt("42");       // String → int (THE method!)
Integer obj = Integer.valueOf(42);    // int → Integer (cached -128..127!)
System.out.println(obj + " max=" + Integer.MAX_VALUE); // 2147483647
System.out.println(Integer.min(10, 20) + " " + Integer.max(10, 20)); // 10 20
System.out.println(Integer.sum(10, 20)); // 30
System.out.println(Integer.toBinaryString(5)); // 101
System.out.println(Integer.toHexString(255));  // ff
System.out.println(Integer.compare(5, 10));    // negative (5 < 10)
int x = obj.intValue(); // unbox

4. Traps

  • parseInt("abc") / parseInt(null) → NumberFormatException / NPE.
  • new Integer(5) deprecated (Java 9+) — use valueOf / autoboxing.
  • Unbox null → NPE: Integer o = null; int i = o; CRASH!

5. Exam Points

  • Parse + cache (127 vs 128) + binary/hex — 7 marks, very frequent.
  • CLI sum program with parseInt — 5 marks, frequent.
Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.

← All Introduction to Java Programming modules