Multiple choice technology programming languages

Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?

  1. Compiler error

  2. Runtime Exception

  3. True

  4. False

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

When you use 'new Integer(2)', you create distinct Integer objects on the heap. The '' operator compares object references, not values. Since 'a' and 'b' point to different objects, 'ab' returns false. To compare Integer values, use a.equals(b).