[Bluej-discuss] concatenating an index value with a note ex 4.15

Bruce Barton bartonb at sunysuffolk.edu
Fri Oct 6 21:31:31 BST 2006


Actually your code, below, is not quite accurate.
 
        "Catch-" + 22
 
is restructured by the compiler into:
 
        new StringBuilder("Catch-").append(22).toString()
 
Within the StringBuilder class there are append methods for each of the primitive types, the String type, the char[] type, and the Object (and its derivative) types.  So the append methods (which in the case of 22 will, as noted, call Integer.toString(22)) will convert the argument to a String and then append the String into the StringBuilder.  The final .toString() is needed to create the immutable String object rather than the mutable StringBuilder object.
 
Bruce Barton
Asst. Professor of Computer Science
Suffolk County Community College

________________________________

From: bluej-discuss-bounces at bluej.org on behalf of Ian Barland
Sent: Wed 10/4/2006 9:45 PM
To: bluej-discuss at bluej.org; bubblez123 at xtra.co.nz
Subject: Re: [Bluej-discuss] concatenating an index value with a note ex 4.15


I try to mention to my students rather early on that when Java sees
   "Catch " + 22
there is an invisible conversion which occurs, calling a function which takes the int 22 and returns a String.
In fact, Java re-writes(*) that short statement behind-the-scenes as
   "Catch ".concat( (new Integer(22)).toString() )
which is more complicated in one sense, but simpler in the sense that it shows that everything reduces to calling methods, without any magical "+" or hidden conversions.  Some students ignore this digression, but for some it helps.

(*) Yes, I realize my translation is a bit of a lie; there is a static Integer.toString(int) method.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.bluej.org/pipermail/bluej-discuss/attachments/20061006/a6833b54/attachment.html


More information about the bluej-discuss mailing list