Question 13
Back to Exercise
What is the output of the following Java program?
public class Main {
static void addOne(Integer n) { n++; }
public static void main(String[] args) {
Integer count = 500;
Integer same = count;
count++;
addOne(count);
System.out.println(count + " " + same + " " + (count == same));
}
}
- A 501 500 false Correct
- B 502 500 false
- C 501 501 true
- D 502 502 true
Correct Answer: Option A
count++ makes count refer to a new Integer, and addOne only changes its own parameter. Wrappers are immutable.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!