Santosh Ramachandrula wrote:So you are saying that "statement 2" will create an extra object in the heap
The code will not compile. However, if you change it so that it will, then the arg to compareTo() statement 1 will always create a new Integer object, and the arg to compareTo() in statement 2 might or might not create a new Integer object.
but the two statements will always return the same value all the time, right?
Yes, which you can tell by reading the documentation for Integer.compareTo()
Santosh Ramachandrula wrote:
When I pass a primitive "int"in the second statement "does Java convert it to Integer object by "auto boxing"?
Your questions will be clearer if all the relevant details are together, rather than having to refer back 5 or 10 posts so see what you're talking about.
Are you asking if the 2 is autoboxed in
If so, the answer is yes. Anywhere an Integer is expected and you provide a primitive int, if it compiles, it's autoboxing. And anywhere a primitive int is expected and you provide an Integer, if it compiles, it auto-unboxing.