This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Strings

 
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();
 
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.)
 
Wanderer
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.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic