• 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 equals

 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class stringEquals{

public static void main(String[] args){
String x1="abc";
String x2="ab";
x2=x2+"c";
if(x1==x2)
System.out.println("both do refer to same object");
if(x1.equals(x2))
System.out.println("both objects have same content ");
}
}


what is the answer to this problem i guessed answer would be
both do refer to same object
both objects have same content

since firstly a x1 reference is refering to object containing "abc"..this object goes to String object pool.
then another object is created that has content as ab and is refered by x2
this object also goes to String object pool

now when the third line excutes another object containing "c" is added to object pool and lost immediately ..then another object is created with contents "abc" and placed in object pool but now JVM sees it has a object containing same literal previously so it does not create a new object instead causes x2 refer to same object as x1...

so x1 has same bit pattern as x2 then why x1==x2 prints out false?


please answer me ..
Regards
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is evident that whenever String operations are performed and assigned to new string objects the referances are not taken from pool.
As in when you do :

String s ="ab"
and s2 = s + "c" --> it is doing new String() for "abc".

Similarly if one were to do :

s2 = s.replace("b","c") --> then internally a new String() is being created,pointing to "ac" string.

-
Shivani.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. Here is one explanation from http://www.jchq.net/tutorial/05_02Tut.htm


String s = "Hello";
String s2 = "Hello";
if (s==s2){
System.out.println("Equal without new operator");
}
String t = new String("Hello");
string u = new String("Hello");
if (t==u){
System.out.println("Equal with new operator");
}

From the previous objective you might expect that the first output "Equal without new operator" would never be seen as s and s2 are different objects, and the == operator tests what an object points to, not its value. However because of the way Java conserves resources by re-using identical strings that are created without the new operator s and s2 have the same "address" and the code does output the string

"Equal without new operator"

However with the second set of strings t and u, the new operator forces Java to create separate strings. Because the == operator only compares the address of the object, not the value, t and u have different addresses and thus the string "Equal with new operator" is never seen.

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree fully with Shivani.

Actually, this is a tricky case of the interned Strings.

The conditions for two Strings to be interned is that they should be compile time constants.

So, a == b gives true for the below:
String a = "abc";
String b = "abc";

However, a == d will give false if we use the Strings as below:
String a = "abc";
String b = "ab";
String c = "c";
String d = b + c;

since d is a totally new object and secondly, it's value is evaluated at runtime.
[ September 10, 2005: Message edited by: Sherry Jacob ]
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, yes. I see. If I wrote . . .

String s = "Hello";
String s2 = "Hell";
s2+="o";
if (s==s2){
System.out.println("Equal without new operator");
}
String t = new String("Hello");
string u = new String("Hello");
if (t==u){
System.out.println("Equal with new operator");
}


. . . then, there would be no output because Java will
not store String s1 and String s2 as one object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic