• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

someone please validate

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am telling, if i say

[1]

String x,y,z;

x= "abc";
y="def"
z=x+y;

I will end up creating 3 string *objects* [ pls correct if it is wrong ] namely "abc" , "def", abcdef" in "String Pool".

Am I right?

[2] "String Pool" contains String *objects* right? or String *literals*?

Please validate my understanding.

thanks
chaitanya
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Java-Language-Specification:
String Concatenation Operator +
If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (�15.28))that is the concatenation of the two operand strings.

So you end up with two String-Objects in the pool and one non-pool-object ("abcdef"). You can verify this with

which will print "false".

"String Pool" contains String *objects* right? or String *literals*?


Strings are always objects!. A literal is just a kind of shortcut, that will be translated from the compiler, and lets you write String s = "abc"; But at runtime there is nothing like a String-literal.
[ May 14, 2007: Message edited by: Sasha Ruehmkorf ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A String literal is a thing that appears in the source code, beginning and ending with ". This results in an actual String object being created at run time, which corresponds to the literal. We may use the terms somewhat interchangably at times, but strictly speaking, the literal exists only until compile time, and the pooled String instance exists only at run time.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


From the Java-Language-Specification:
String Concatenation Operator +
If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a String object (newly created, unless the expression is a compile-time constant expression (�15.28))that is the concatenation of the two operand strings.

So you end up with two String-Objects in the pool and one non-pool-object ("abcdef"). You can verify this with




Why is "abcdef" not created in the pool too...shouldnt there be 4 objects in total...Also i didnt understand this quite well..Please help me get this...

Thanks,
Megha
 
Crusading Chameleon likes the size of this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic