• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Literal Pool after Concatenation

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Every one,
I read in various books, forums and believed that irrespective of string manipulation on literals string == operator will always return true, here is a very simple program and its output i want to to know why string1==string4 returns false ? even though both are string literals. Does it mean a string concatenation on string literals will return a new object ?

Runs Output:
string1= helloWorld
string2= helloWorld
string3= hell
string4= helloWorld
string1==string2 is true
string1==string4 is false

Thanks.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference is between compile time and runtime.
If a string expression can be evaluated at compile time, it will be put into the string pool.
It will evaluate the concatenation it can at compile time.

In this specific example, the concatenation of string5 and string6 is done at runtime (string5 is not constant at compile time) and thus results in a new string.

However consider the following examples:



 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic