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
posted
0
<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
posted
0
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
posted
0
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
posted
0
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: 18670
posted
0
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.