LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Predict the Output · Question 13

Incrementing an Integer creates a new object Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links