Originally posted by Ram Reddy:
But
I am sending the Integer object in line1. So while evaluating the expression JVM won't call toString() method for 'i' in line1.
When i called explicitly , i got the expected output.
rami
In line 1 the Integer object's toString method isn't called at all, that's where your confusion comes in. Rather that Integer object's intValue is called, which you'll see if you put a breakpoint in the code and step through it.
That's boxing and unboxing, the sneakiness of treating an Integer as an int where appropriate and an int as an Integer.
In line 2 it's a String, because you called toString, no sneaky boxing for Strings.