• 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

"Hello" strings

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All:
Here's some code:


Here is the result of running this code:
Hello
Hello = Hello
true
false

I'm just simply looking for confirmation of my reason why I think line 4 printed "false". I"m thinking it has something to do with immutable String objects, and because the following is true (from K&B SCJP 6, page 430 431)


I'm thinking that on line 1 the println statement resulted in a concatenation of the two string objects in order to complete the println command, however this same oncatenation does not happen in the code on line 4. Is this true? And if true, what will the JVM in fact be comparing the hello String object to, will it be the "Hel" character string alone or the lo String object alone, or something else?

Thank you for your time.
Gary
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The simple answer is that on string concatenation, a new String object is created, so it is in a different location on the memory, so it wont be "==" to the old String, but only "equals".
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ajezi Sajirava wrote:The simple answer is that on string concatenation, a new String object is created, so it is in a different location on the memory, so it wont be "==" to the old String, but only "equals".




Agreed. The "simple answer" is to use the equals() method. Any more than that will require knowledge of the string pool and strings as compile time constants.

Henry
 
Gary Marshall
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

OK, thank you.

Then why is it that line3 returns "true"? Doesn't the code "..... ("Hel"+"lo"))" create a new String object, and therefore shouldn't the code

System.out.println(hello == ("Hel"+"lo")); also return false?

Thank you
Gary
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 3 returns true because "Hel" + "lo" ,which finally results in "Hello" , would refer to the existing object in String pool.
Someone please correct me if I am wrong.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String doubt

This question has been discussed here in more detail
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Max White wrote:Line 3 returns true because "Hel" + "lo" ,which finally results in "Hello" , would refer to the existing object in String pool.
Someone please correct me if I am wrong.



Correct. ("Hel" + "lo") is a compile time constant expression, which becomes "Hello", which in turn, shares the same copy due to the string pool.

Henry
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Ajezi Sajirava
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic