Overstock.com Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
== operator is used to compare two object reference values....but 'equals operator is used to compare two contents which is stored in objects.
eg:
String s1=new String("welcome");
String s2=new String("welcome");
if(s1==s2)
{
System.out.println("two strings are equal");
}
else
{
System.out.println("two strings are not equal");
}
in above example we are using '=='operator so,it gives output is "two strings are not equal".
inplace of '=='operator we are using 'equals'
if(s1.equals(s2))
then it give output as "two strings are equal"
== operator is used to compare two object reference values....but 'equals operator is used to compare two contents which is stored in objects.
eg:
String s1=new String("welcome");
String s2=new String("welcome");
if(s1==s2)
{
System.out.println("two strings are equal");
}
else
{
System.out.println("two strings are not equal");
}
in above example we are using '=='operator so,it gives output is "two strings are not equal".
inplace of '=='operator we are using 'equals'
if(s1.equals(s2))
then it give output as "two strings are equal"
== operator is used to compare two object reference values....but 'equals operator is used to compare two contents which is stored in objects.
eg:
String s1=new String("welcome");
String s2=new String("welcome");
if(s1==s2)
{
System.out.println("two strings are equal");
}
else
{
System.out.println("two strings are not equal");
}
in above example we are using '=='operator so,it gives output is "two strings are not equal".
inplace of '=='operator we are using 'equals'
if(s1.equals(s2))
then it give output as "two strings are equal"
== compares the object reference while equals compares value of object.
ex.
here output will be "equal" but if we use == then answer will be not equal.
- Anonymous December 19, 2012