Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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:



 
You showed up just in time for the waffles! And this 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
reply
    Bookmark Topic Watch Topic
  • New Topic