Originally posted by Pauline McNamara:
I had actually been looking around for something that went into what happens with something like:
1) int dogsInMyHouse = 3 ; // I wish!
System.out.print( dogsInMyHouse ) ;
The original question really was: why don't we need to do
2) System.out.print( dogsInMyHouse.toString() ) ;
Because System.out is an object of type PrintStream, I looked in the PrintStream API. I see that the print() method (and the println() method) is overloaded to accept all sorts of input including primitives like int as well as Objects and Strings.
You can't call toString() on a primitive, so your second example will not work in any jdk less than v5. In jdk 5 it will work, not because it makes dogsInMyHouse into a String, but because autoboxing will convert the int to an Integer and call toString() on the Integer.
Regarding the article (last updated in March, 2004), I think it has some good points. However, I have an issue with his/her statement, "You may not realize it, but each time a concatenation occurs, a new string is created. This is considered slow. ... As a programmer, you don't have to care (at least not now)."
I also think that there is some minor misinformation in the Concatenation and Mixed Types section. My curiosity is aroused, so I'm going to do some experimenting and then I'll pass along the results.
[ January 03, 2007: Message edited by: Marilyn de Queiroz ]