Hi all,
String s1 = new String("foo");
String s2 = new String("foo");
These statements create
two String objects in the
heap both of which contain the unicode characters for the
word "foo".
's1' and 's2' are
reference type variables. They
are not objects.
String s1 = "foo";
String s2 = "foo";
The above creates
one String object in the constant-pool; "foo" and two reference type variables that both hold the memory address for the same "foo" string object.
String literals use the pool; Strings calculated at runtime ie using
new or '+' are set at compile time.
Primitive variables ie boolean, int, long, etc are stored in their own area of memory and have pre-defined sizes which are known at compile-time.
The size of an object cannot be known precisely at runtime, so a reference type variable is used. A reference type variable stores the
memory address for an oject in memory. A memory address is a set size ie 16-bit, 32-bit or 64-bit ... the compiler always knows how much memory it requires to hold it.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited April 30, 2001).]