Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic