Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Two identical Strings stored in two different object

Thread view: 
Neroku - 12 Feb 2007 00:21 GMT
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
Arne Vajhøj - 12 Feb 2007 00:31 GMT
> Hi, I know java ensures that only one String object exists when there
> are two identicals Strings, i.e:
[quoted text clipped - 15 lines]
> 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
Lew - 12 Feb 2007 01:09 GMT
Neroku wrote:
>> Hi, I know java ensures that only one String object exists when there
>> are two identicals Strings, i.e:

Only for literals and one other case, mentioned below.

>> String s1 = "Hello";
>> String s2 = "Hello";
[quoted text clipped - 12 lines]
>> I don't understand why, since both Strings hold the same data. Any
>> ideas?

> "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
Karl Uppiano - 12 Feb 2007 01:18 GMT
> Neroku wrote:
>>> Hi, I know java ensures that only one String object exists when there
[quoted text clipped - 39 lines]
>
> - Lew

I think they use the same character buffer though?
Oliver Wong - 12 Feb 2007 15:26 GMT
[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
Corona4456 - 12 Feb 2007 02:08 GMT
> Hi, I know java ensures that only one String object exists when there
> are two identicals Strings, i.e:
[quoted text clipped - 17 lines]
>
> 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)
Lew - 12 Feb 2007 02:48 GMT
"Neroku" <n37...@gmail.com> wrote:
>> Hi, I know java ensures that only one String object exists when there
>> are two identicals Strings, i.e:
[quoted text clipped - 17 lines]
>>
>> 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)

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
Arne Vajhøj - 12 Feb 2007 02:52 GMT
>> Hi, I know java ensures that only one String object exists when there
>> are two identicals Strings, i.e:
[quoted text clipped - 19 lines]
> 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
bigbighd604 - 12 Feb 2007 02:26 GMT
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.

> Hi, I know java ensures that only one String object exists when there
> are two identicals Strings, i.e:
[quoted text clipped - 17 lines]
>
> TIA
Lew - 12 Feb 2007 02:51 GMT
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";
[quoted text clipped - 4 lines]
> 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
Arne Vajhøj - 12 Feb 2007 02:56 GMT
> Java has a special optimize mechanism for declaring a new String
> Object using the format 'String sx = "xxx"', it will try to find
[quoted text clipped - 6 lines]
> 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
Oliver Wong - 12 Feb 2007 15:32 GMT
>> 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
Mike Schilling - 12 Feb 2007 05:28 GMT
> 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:
>
[quoted text clipped - 3 lines]
>
> It prints "true"

Yes, these are literal Strings.

> However:
>
[quoted text clipped - 3 lines]
>
> 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".


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.