3
System.out.println(Integer.valueOf(5) == Integer.valueOf(5)); System.out.println(Integer.valueOf(500) == Integer.valueOf(500)); 

The output is

true false 

Why does the first line returns true but the second line returns false? What's the trick here since both are calling valueOf() on Integer class.

4

1 Answer 1

4

There's cached instances of low numbered Integer objects but not any of higher valued Integer objects.

If you didn't notice before, you are comparing objects, not ints.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.