aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
Can anyone, tell me what does the following code mean:
String c="a";
String d = (c + "aaa").intern();
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
<code>String c="a";</code>
// assignees a reference to the String "a" to c.
<code>String d = (c + "aaa").intern();</code>
// concatenates "a" and "aaa", creates String "aaaa"
// places in tnto VM internal string pool
// assignees a reference to String "aaaa" to d.
check what will happened with this code:
1.
<code>
String c="a";
String d = (c + "aaa").intern();
String e = "aaaa";
System.out.print(e=d)
</code>
1.
<code>
String c="a";
String d = (c + "aaa").intern();
String e = "aaaa";
System.out.print(e=d)
</code>
read about String class in

Java Platform specification
Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185
should be:
2.
<code>
String c="a";
String d = (c + "aaa");
String e = "aaaa";
System.out.print(e=d)
</code>

Michal Harezlak
Ranch Hand

Joined: Jul 06, 2000
Posts: 185

and one more :-(((
2.
<code>
String c="a";
String d = (c + "aaa");
String e = "aaaa";
System.out.print(e==d)
</code>
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by Michal Harezlak:
<code>String c="a";</code>
// assignees a reference to the String "a" to c.
<code>String d = (c + "aaa").intern();</code>
// concatenates "a" and "aaa", creates String "aaaa"
// places in tnto VM internal string pool
// assignees a reference to String "aaaa" to d.
check what will happened with this code:
[b]1.

<code>
String c="a";
String d = (c + "aaa").intern();
String e = "aaaa";
System.out.print(e=d)
</code>
1.
<code>
String c="a";
String d = (c + "aaa").intern();
String e = "aaaa";
System.out.print(e=d)
</code>
read about String class in

Java Platform specification
[/B]


Thanks for ur reply! Does that mean that now d and c will
now have the same reference (since VM places d in the same
string pool.)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Exactly.
Michal- if you register for a JavaRanch account, you will be able to edit your messages after you post them, which as you see can be useful at times.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings