kaeli said:
It is my understanding that if you don't specify the "new" keyword, if
another string object already has a reference to the value "abc", a new
object is not created (no more memory is allocated)
It's a little different than that. There is a pool of String objects
maintained by the JVM. Some String objects are in that pool, and others
are not. When you say:
String a = "abc";
then the JVM arranges for "abc" to be in that pool, and then produces a
reference to it when executing that code. When you write:
String a = new String("abc");
then the JVM still arranges for "abc" to be in that pool, and still
provides a reference to it, but that reference is then passed into a
constructor to create a NEW String object that's NOT in the pool and
copies the first one. It's that second String object which ends up
being assigned to the reference.
This is different from your statement above. If a String object exists
that contains the same contents -- abc -- but the object is NOT in the
pool (if, for example, it was read from a file or database), then the
literal "abc" will resolve to a new String object that's different from
that existing one.
It's also a little imprecise to says that the pooled String objects are
created by the code above. Instead, it's better to say that the code
above ensures that at some point, the JVM will arrange for the String
object to have been created when that code executes. However, that
object will be created only once, regardless of how many times the code
is executed, or even how many times the literal "abc" appears in the
code.
Mostly the difference is seen with the equals method and '=='.
Yes. Use of '==' is, in fact, the only direct way to see the
difference. Indirectly, of course, you could observe the memory usage
of the application after a large number of repetitions of the code.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation