• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

String == operator

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain the why the output is false for the following code ?



The output is false that means string created at LINE X has different bits than str1. I thought that if the same string (in our case "SCJP") is available in String constant pool, then str5 would simply be pointed to str1, that would save memory isn't it ?

Am I thinking wrong here ?

TIA
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A String MAY be taken from the String constant pool, but it doesn't have to be.
Therefore the behaviour of == on Strings (like on any other object) will NOT be predictable unless you're intent on comparing actual references.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeroen,
Thanks for the reply.I tried various combinations of string comparison and here is what I observed.


Whenever the == operator is operating on string objects which perform concatenation using + operator, the result is false, else the result is true.



Guys, this is my observation feel free to point out any combination which might prove this wrong.

Below is the code I wrote to test this, hopefully my fellow ranchers might benefit from this.


NOTE: the line x will result in true, because the concat function in String.java will check for string length which is zero in this case and will simply return this which is str6 object itself.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think when you concatinate Str2 and Str23, reference of Str5 is different than Str1. If you try to compare Str5 with Str1 by "==", it will result "false". but with Str1.equals(Str5), it will return true.

Plz Correct me if i'm wrong.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are absolutely correct Hai Le, equals will give true
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't work because str5 isn't a string literal and has no relation to the string literal pool.

According to this...


When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those who are interested may like to compile and run this:



Note line Y and the ouput. The intern method was discussed fairly recently in this forum.
 
pie. 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