CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS J2EE JAVA DETAILS
Question :
How is it possible for two String objects with identical values not to be equal under the == operator?
Category Java Interview Questions
Rating (4.1) By 36 users
Added on 10/13/2004
Views 1757
Rate it!
Answers:

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.



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.



Posted by: DEVARATHNAM C, KOTAGUDIBANDA(PO),KALAKADA(MD),CHIT    

Contact DEVARATHNAM C, KOTAGUDIBANDA(PO),KALAKADA(MD),CHIT 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");
}

}
}



Posted by: Amit Rane    

Contact Amit Rane 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



Posted by: Goutham    

Contact Goutham 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



Posted by: faraz    

Contact faraz 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.
  • Name :*
    Email Id :*
    Answer :*
    Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
    Process Verification  Enter the above shown code:*
    Inform me about updated answers to this question

       
    Related Questions
    View Answer
    How does multithreading take place on a computer with a single CPU?
    View Answer
    What restrictions are placed on method overloading?
    View Answer
    What restrictions are placed on method overriding?
    View Answer
    What happens when a thread cannot acquire a lock on an object?
    View Answer
    What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
    View Answer
    What classes of exceptions may be caught by a catch clause?
    View Answer
    What is the purpose of finalization?
    View Answer
    What is synchronization and why is it important?
    View Answer
    What are synchronized methods and synchronized statements?
    View Answer
    What is the serialization?
    View Answer

    Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

    Notify me when better answer is posted!
    Email:

    View ALL Java Interview Questions

    User Options
    Sponsored Links


    Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
    Privacy Policy | Terms and Conditions
    Page URL: http://www.coolinterview.com/interview/359/default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

    0.92
    Cache = 0 Seconds