Doubt in Strings

P

Prasoon

Considering the statements

a) String s1="Hello";


b) i ) String s="Hello";

and

ii) String s=new String("Hello");

I thought both the statements b(i) and b(ii) were identical before I
came to know that "object creation" is guaranteed in b (ii) whereas it
is not guaranteed in b(i) after statement (a) has been executed!!!!

Am I correct???
Please make it clear for me.....

Anyways is there anything mentioned about the "String Pool" in the
JLS?????
 
J

John B. Matthews

Prasoon said:
Considering the statements

a) String s1="Hello";


b) i ) String s="Hello";

and

ii) String s=new String("Hello");

I thought both the statements b(i) and b(ii) were identical before I
came to know that "object creation" is guaranteed in b (ii) whereas
it is not guaranteed in b(i) after statement (a) has been
executed!!!!

Am I correct??? Please make it clear for me.....

<code>
public class InternTest {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String(str1).intern();
String str3 = new String(str1);
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}
}
</code>

<output>
true
false
</output>

NB: Comparing strings using "==" is not usually the desired test; of
course, all three string match via equals().
Anyways is there anything mentioned about the "String Pool" in the
JLS?

The JLS uses the term "interned" in the context of "strings that are the
values of constant expressions":

<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5>

In contrast, the API uses the term "pool when discussing intern():

<http://www.j2ee.me/javase/6/docs/api/java/lang/String.html#intern()>
 
N

neuneudr

<code>
public class InternTest {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String(str1).intern();
String str3 = new String(str1);
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}}

</code>

Hi John and all,

but regarding the OP's question, concerning "object creation" [sic]...

In your:

String str2 = new String(str1).intern();

you're still creating a new object, that shall be
eligible for GC right after the call to intern() no?

Which is quite different from how String literals are interned,
at least from the OP's "object creation" point of view?

Driss
 
J

John B. Matthews

<code>
public class InternTest {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = new String(str1).intern();
String str3 = new String(str1);
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}}

</code>

Hi John and all,

but regarding the OP's question, concerning "object creation" [sic]...

In your:

String str2 = new String(str1).intern();

you're still creating a new object, that shall be
eligible for GC right after the call to intern() no?

The expression "new String(str1).intern()" includes two steps: 1) An
anonymous new copy of str1 is created; and 2) that copy is found among
the interned Strings, and a reference to it is returned in str2. As there
is no outstanding reference to the intermediate copy, it is eligible for
collection; but the interned String, referenced by str2, is ineligible
until it goes out of scope or is explicitly nulled:

Which is quite different from how String literals are interned,
at least from the OP's "object creation" point of view?

I don't see this. The reference in str1 is obtained by loading a constant
determined at compile time. The reference in str2 is returned by
intern(). These two references are identical. The reference in str3 is
obtained by invoking the relevant String constructor, which creates a
copy. As a result, the reference in str3 is different.
 
J

Joshua Cranmer

Prasoon said:
Considering the statements

a) String s1="Hello";


b) i ) String s="Hello";

and

ii) String s=new String("Hello");

I thought both the statements b(i) and b(ii) were identical before I
came to know that "object creation" is guaranteed in b (ii) whereas it
is not guaranteed in b(i) after statement (a) has been executed!!!!

No, they are not equivalent. In terms of the produced bytecode, the
former would be an ldc followed by an astore; the latter would be an
anew, dup, ldc, invokespecial, aload. The ldc causes reuse of the string
literal (specifically, it is an interned string representation), but the
anew definitely causes creation of a new object, which may of course
cause memory to run out. However, unless I am mistaken, the new String
will share the same character buffer, so if that causes an
OutOfMemoryError, chances are that your program is about to run out of
memory anyways.
Anyways is there anything mentioned about the "String Pool" in the
JLS?????

JLS §3.10.5 String Literals:
Each string literal is a reference (§4.3) to an instance (§4.3.1,
§12.5) of class String (§4.3.3). String objects have a constant value.
String literals-or, more generally, strings that are the values of
constant expressions (§15.28)-are "interned" so as to share unique
instances, using the method String.intern.

That is as much detail as it goes into, since the Java developers
decided eons ago that it would be more productive to separate the core
Java API (especially java.lang) from the language specification itself.
The String.intern() method documentation is the component which actually
describes the pool:

A pool of strings, initially empty, is maintained privately by the class
String.

When the intern method is invoked, if the pool already contains a string
equal to this String object as determined by the equals(Object) method,
then the string from the pool is returned. Otherwise, this String object
is added to the pool and a reference to this String object is returned.
 
A

Arne Vajhøj

Prasoon said:
Considering the statements

a) String s1="Hello";


b) i ) String s="Hello";

and

ii) String s=new String("Hello");

I thought both the statements b(i) and b(ii) were identical before I
came to know that "object creation" is guaranteed in b (ii) whereas it
is not guaranteed in b(i) after statement (a) has been executed!!!!

Am I correct???
Please make it clear for me.....

Anyways is there anything mentioned about the "String Pool" in the
JLS?????

If you are interested in the subtleties of Java, then study the
previous answers.

If you are interested in writing Java code, then use option i.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top