• 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

Comparing output that deals with strings

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My struggle with understanding objects continues:

Output:
Hello
Hello
one
three
four


First Question: why do lines 5 and 8 both produce output, and yet lines 6 and 7 do not both produce output?

Second Question: why does line 8 produce output, I thought that the == operator only worked for comparing primitives, not strings? Or does it work for comparing objects (and in this case, we're comparing objects that are of type string)?

Third Question: assuming that == does indeed work for comparing objects, why does line 8 produce output if we're comparing the value "Hello" with the s object?
 
Rancher
Posts: 175
7
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equals compares content of the string (text) while == compares if both references (variables) point to the same object. Now, the confusion comes from the fact that in Java Strings are interned which causes == to return true in some cases - basically if you create bunch of strings with the same value, the values will be stored in the intern pool... but not when you create your string using a "new" keyword.
 
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Adrian Grabowski wrote:. . . if you create bunch of strings with the same value, the values will be stored in the intern pool... . . . .

Please explain more; I am not sure that is quite accurate.
 
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Adrian Grabowski wrote:. . . if you create bunch of strings with the same value, the values will be stored in the intern pool... . . . .

Please explain more; I am not sure that is quite accurate.

when you Creat a string object with "hello" and if you try Creat another string object with the same content then compiler first look into the string literal pool that the object you created is already there in pool are not that is called interned object, if it finds the same content of your object it will not create new object instead it assign the new reference to the same object that mean one object two references
 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . when you Creat a string object with "hello" and if you try Creat another string object with the same content then compiler first look into the string literal pool . . .

That is not correct, I am afraid. automatic interning of Strings only happens when the Strings are compile‑time constants.
 
sohail hussain
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

sohail hussain wrote:. . . when you Creat a string object with "hello" and if you try Creat another string object with the same content then compiler first look into the string literal pool . . .

That is not correct, I am afraid. automatic interning of Strings only happens when the Strings are compile‑time constants.

but string is implicitly final  and extends directly the Object class so where is the concept of compile time constant
How ever compile time constant mean variables must be final so they should not change there values at runtime
 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A final variable initialised to an expression which is a compile time constant becomes a compile time constant itself. But it is possible to have Strings which have identical content and are not the same instance and are not interned. Assume there has been no previous method call and the Scanner reads the whole of the next line.So, you enter “Campbell Ritchie” for lines 3 and 6. You now have two Strings which will return true from equals() and are not compile time constants and are not interned. Use of == will evaluate to false.
Please have a look at the old article by Corey McGlone. Even though it is old, it is still mostly accurate, and it explains when Strings are and are not interned. The following code will have a different effect, particularly if the two inputs are the same:-The String#intern() method attempts to put its object into a pool; if an identical object (by equals()) already exists in the pool, it returns that object instead. Objects in the pool returning true from equals() will also have == evaluate to true.

But remember, Strings are only put in the pool if they are compile time constants or you explicitly call intern() on their references.
 
    Bookmark Topic Watch Topic
  • New Topic