This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone, tell me what does the following code mean:
String c="a";
String d = (c + "aaa").intern();
 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should be:
2.
<code>
String c="a";
String d = (c + "aaa");
String e = "aaaa";
System.out.print(e=d)
</code>

 
Michal Harezlak
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and one more :-(((
2.
<code>
String c="a";
String d = (c + "aaa");
String e = "aaaa";
System.out.print(e==d)
</code>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic