Two identical Strings stored in two different object

N

Neroku

Hi, I know java ensures that only one String object exists when there
are two identicals Strings, i.e:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1==s2);

It prints "true"

However:

String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1==s2);

prints "false"

I don't understand why, since both Strings hold the same data. Any
ideas?

TIA
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Neroku said:
Hi, I know java ensures that only one String object exists when there
are two identicals Strings, i.e:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1==s2);

It prints "true"

However:

String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1==s2);

prints "false"

I don't understand why, since both Strings hold the same data. Any
ideas?

"Hello" and "Hello" becomes one object.

new String("Hello") means create a new object and
allocate memory for that and copy the input string
to it.

I am not sure that Java require it to be another object,
but from a practical implementation point of view
I consider it very natural that it is.

Try consider writing the Java code for that constructor.

Arne
 
L

Lew

Only for literals and one other case, mentioned below.
"Hello" and "Hello" becomes one object.

Because both are literals.
new String("Hello") means create a new object and
allocate memory for that and copy the input string
to it.

Because it isn't a literal.
I am not sure that Java require it to be another object,
but from a practical implementation point of view
I consider it very natural that it is.

Try consider writing the Java code for that constructor.

Read about the String.intern() method:
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern()>

- Lew
 
C

Corona4456

Hi, I know java ensures that only one String object exists when there
are two identicals Strings, i.e:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1==s2);

It prints "true"

However:

String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1==s2);

prints "false"

I don't understand why, since both Strings hold the same data. Any
ideas?

TIA

Java is a reference language so when you call the '==' it compares
addresses not values. If you want to compare Strings use the equals
function (i.e. System.out.println(s1.equals(s2)) should print true)
 
B

bigbighd604

Java has a special optimize mechanism for declaring a new String
Object using the format 'String sx = "xxx"', it will try to find
whether already has a String Object represents "xxx", if there then
return that Object's reference or create a new one. But the format
'new String("xxx")' will always create a new String Object.

so when you type:
String s1 = "Hello";
Stting s2 = new String("Hello");
There will be two String objects.

String s1 = new String("Hello");
String s2 = "Hello";
You will get the same String Ojbect.
 
L

Lew

Java is a reference language so when you call the '==' it compares
addresses not values. If you want to compare Strings use the equals
function (i.e. System.out.println(s1.equals(s2)) should print true)

That's not the issue. The OP was discussing the interning of Strings. At issue
is object equality, not value equality, hence == is what is needed.

- Lew
 
L

Lew

Please do not top post.
Java has a special optimize mechanism for declaring a new String
Object using the format 'String sx = "xxx"', it will try to find
whether already has a String Object represents "xxx", if there then
return that Object's reference or create a new one. But the format
'new String("xxx")' will always create a new String Object.

Correct. Where "it" looks for these strings is in a private store within the
String class.
so when you type:
String s1 = "Hello";
Stting s2 = new String("Hello");
There will be two String objects.

String s1 = new String("Hello");
String s2 = "Hello";
You will get the same String Ojbect.

You were right the first time, but wrong the second. Both examples will yield
distinct String objects for s1 and s2.

- Lew
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Corona4456 said:
Java is a reference language so when you call the '==' it compares
addresses not values. If you want to compare Strings use the equals
function (i.e. System.out.println(s1.equals(s2)) should print true)

That is not really what the original poster is asking about.

Arne
 
G

Guest

bigbighd604 said:
Java has a special optimize mechanism for declaring a new String
Object using the format 'String sx = "xxx"', it will try to find
whether already has a String Object represents "xxx", if there then
return that Object's reference or create a new one. But the format
'new String("xxx")' will always create a new String Object.

so when you type:
String s1 = "Hello";
Stting s2 = new String("Hello");
There will be two String objects.
Yep.

String s1 = new String("Hello");
String s2 = "Hello";
You will get the same String Ojbect.

Are you sure ?

Arne
 
M

Mike Schilling

Neroku said:
Hi, I know java ensures that only one String object exists when there
are two identicals Strings

No, Java doesn't ensure that for all strings. It does have a collections of
unique Strings, but not all Strings belong to it. Literal Strings do, and
the return value from String.intern() does.
, i.e:

String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1==s2);

It prints "true"

Yes, these are literal Strings.
However:

String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1==s2);

prints "false"

s2 is not a literal String. Note that "new" is guaranteed to create a
brand-new object, so it would be quite incorrect for this to be true.

But

String s3 = s2.intern();
System.out.println(s1==s3)

will once again be "true".
 
O

Oliver Wong

[on the topic of "Hello" and new String("Hello")]
I think they use the same character buffer though?

Perhaps they do, but the == operator doesn't compare character buffers.
It compares object references, and the two references refer to different
objects.

- Oliver
 
O

Oliver Wong

Arne Vajhøj said:
bigbighd604 said:
Java has a special optimize mechanism for declaring a new String
Object using the format 'String sx = "xxx"', it will try to find
whether already has a String Object represents "xxx", if there then
return that Object's reference or create a new one. But the format
'new String("xxx")' will always create a new String Object. [...]
String s1 = new String("Hello");
String s2 = "Hello";
You will get the same String Ojbect.

Are you sure ?

<SSCCE>
public class Test {
public static void main(String[] args) {
String s1 = new String("Hello");
String s2 = "Hello";
System.out.println(s1 == s2);
}
}
</SSCCE>

<output>
false
</output>

The "special optimization mechanism" happens at compile time. The
compiler adds a "constant pool" to every .class file it generates, storing
any constants (string literals, among other things) that class file needs,
and only stores one copy for constants that appear repeatedly in the source.
Allocating the string, via the "new" operator, happens at runtime.

- Oliver
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top