We know that when we use objects in sysout(System.out.Println) statement internally it’s toString method is called. and with primitive it directly prints. but when we use any Wrapper class type of objects is used say suppose Integer like following
Integer i = new Integer(10)
System.out.Println(i);
is it toString() is responsible for printing it or Unboxing?
A quick test and debug run shows that
print(Object)is invoked, notprint(int).A good way to check this is :
If un-boxing were used, this would throw a NullPointerException. This doesn’t happen, though, it prints the string
null, which is the output ofString.valueOf(Object)when a null is passed in.Another aspect to bear in mind is that
PrintStreamexisted before Java 5. When autoboxing was introduced in Java 5, it had to be ensured that any existing code usingPrintStreamwould not suddenly have its behaviour changed. So any existing code callingprint(Object)must not suddenly have its behaviour changed to callprint(int)instead, simply because of a new language feature. Backwards compatibility must always be maintained.