Question:
How is it possible for two String objects with identical values not to be equal under the == operator?
Answer:
The == operator compares two objects to determine if they are the same object in memory. It is possible for two String objects to have the same value, but located indifferent areas of memory. Source: CoolInterview.com
Hi... Actually the "=="operator compares the object references(memory locations).So we are getting false in this case.Observe the following program for more details.
public Class StringTest{ public static void main(String args[]){ String s1="DEVARATHNAM"; String s2="DEVARATHNAM"; }//main }//class Explanation: When u declare as String s1="DEVARATHNAM"; // It creates one object in the String pool with identity like 1001(For example). here "s1" reference variable pointing to the "1001" memory location.i.e s1--->1001. In the similar fashion ,"s2" object also created in the String pool with identity like 1002(For example).here "s2" reference variable pointing to the "1002" memory location.i.e s2--->1002. Now u can compare thewe two objects . if (s1==s2)//it means if(1001==1002) so the condition is false.we are getting the inequality of two string objects,eventhough the content is same. Source: CoolInterview.com
Answered by: DEVARATHNAM C, KOTAGUDIBANDA(PO),KALAKADA(MD),CHIT | Date:
| Contact DEVARATHNAM C, KOTAGUDIBANDA(PO),KALAKADA(MD),CHIT
I don't agree that two strings when compared without creating a new instance are different.
Two string are treated as different if and only if they are created using the new keyword and a constructor.
This code clears it all:
class stringcompare { public static void main(String args[]) { String s1="amit"; String s2="amit";
String s3=new String("amit"); String s4=new String("amit");
if(s1==s2) { System.out.println("s1 and s2 are same"); } else { System.out.println("s1 and s2 are different"); }
if(s3==s4) { System.out.println("s3 and s4 after using constructor are same"); } else { System.out.println("s3 and s4 after using constructor are different"); }
} } Source: CoolInterview.com
Answered by: Amit Rane | Date: 10/6/2007
| Contact Amit Rane
Yes i agree with Amit thats the write answer to the quetion it just compare the address first ie
class stringcompare { public static void main(String args[]) { String s1="amit"; String s2="amit"; if(s1==s2) { System.out.println("both are same"); else System.out.println("both are different"); } } } output Both are same Because string comparison happens whether both address are same or not Source: CoolInterview.com
Answered by: Goutham | Date: 4/17/2010
| Contact Goutham
Yes ,it possible for two String objects with identical values not to be equal under the == operator..!!
class stringcompare { public static void main(String args[]) { String s1="amit"; String s2="amit"; String s3=new String("amit"); if(s1==s2) { System.out.println("both are Same"); } if(s1==s3) { System.out.println("Comparing But doesnt satisfy == operator"); } } //output both are Same Source: CoolInterview.com
Answered by: faraz | Date: 8/15/2010
| Contact faraz
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|