== String comparison [modified]
-
Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:
modified on Thursday, February 11, 2010 8:38 AM
-
Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:
modified on Thursday, February 11, 2010 8:38 AM
As you guess the objects are the same, as they both point to the exact same constant; this is the Java compiler optimising your code. Then when you add another character to
a
it becomes a new object and is no longer the same asb
, even though, in human terms it has not changed.txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
As you guess the objects are the same, as they both point to the exact same constant; this is the Java compiler optimising your code. Then when you add another character to
a
it becomes a new object and is no longer the same asb
, even though, in human terms it has not changed.txtspeak is the realm of 9 year old children, not developers. Christian Graus
-
Hi All, I have a doubt. I was just trying out some basic things in Core Java. Here is the code public class Test { public static void main(String args[]) { String a = "Hai"; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } I Assumed that the result would be "Not Equal" as == does reference comparison. But I got the answer as "Equal". Then I tried the second code public class Test { public static void main(String args[]) { String a = "Hai"; a += ""; String b = "Hai"; if(a == b) { System.out.println("Equal"); } else { System.out.println("Not Equal"); } } } Now i could get "Not equal". But logically both codes are same. Why is this so?? Does Java share similar Objects?? Can anyone give an explanation on this?? Thanks, Annns...:confused:
modified on Thursday, February 11, 2010 8:38 AM
To add to Richards earlier answer,
==
checks that the objects are the same andequals(Object)
should be overriden for ALL classes and checks that the contents are the same [this is the case withString
]. Try this to see:public class TestEquals
{
public static void main(String args[])
{
// Make sure we have two DIFFERENT objects:
String a = new String("Hai");
String b = new String("Hai");if(a == b) { System.out.println("a == b true"); } else { System.out.println("a == b false"); } if(a.equals(b)) { System.out.println("a.equals(b) true"); } else { System.out.println("a.equals(b) false"); }
}
}
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H
-
To add to Richards earlier answer,
==
checks that the objects are the same andequals(Object)
should be overriden for ALL classes and checks that the contents are the same [this is the case withString
]. Try this to see:public class TestEquals
{
public static void main(String args[])
{
// Make sure we have two DIFFERENT objects:
String a = new String("Hai");
String b = new String("Hai");if(a == b) { System.out.println("a == b true"); } else { System.out.println("a == b false"); } if(a.equals(b)) { System.out.println("a.equals(b) true"); } else { System.out.println("a.equals(b) false"); }
}
}
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H
Nagy Vilmos wrote:
// Make sure we have two DIFFERENT objects: String a = new String("Hai"); String b = new String("Hai");
Thanx...You are right. We should make sure that we make two different objects. But I have a doubt in my mind....both a and b should refer to same objects if we write b = a...why they are refering to same object if we say a="Hai" and b="Hai"??
-
Nagy Vilmos wrote:
// Make sure we have two DIFFERENT objects: String a = new String("Hai"); String b = new String("Hai");
Thanx...You are right. We should make sure that we make two different objects. But I have a doubt in my mind....both a and b should refer to same objects if we write b = a...why they are refering to same object if we say a="Hai" and b="Hai"??
What I think you are asking is why, when we write...
String a="Hai";
String b="Hai";...are
a
andb
the same object and not just the same value? When you use a literal string, there will only be one created unless you explicitly create a new instance. m Think of the literal as a constant referencing an object.
Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H