equals vs ==

G

Guest

if (s instanceof String) {
if (s == "a string");
if (s.equals("a string"));
}

what is the difference between s.equals and s == ???

When I use s == "something", many times if s == "something", returns false!

Instead s.equals works correct always.

Is there something that I dont know?
 
J

Joona I Palaste

if (s instanceof String) {
if (s == "a string");
if (s.equals("a string"));
}
what is the difference between s.equals and s == ???
When I use s == "something", many times if s == "something", returns false!
Instead s.equals works correct always.
Is there something that I dont know?

== compares identity, equals() compares equality. Two object references
will only ever return true from == if they refer to the *EXACT* *SAME*
object. "Exact same" meaning there being only one object, with two
references referring to it. OTOH, equals() will return also true if the
references refer to two objects that are considered equal. The
consideration depends on the object class. It defaults to being the
same as ==. String overrides it by comparing the contents of the string.
Other objects can override it in other ways.
 
P

Phil Staite

s.equals(x);

Will return true if the strings s and x contain the same character
sequence. So:

String s = new String("foobar");
String x = new String("foobar");

Then s.equals(x) will return true, but s == x will be false, because s
and x are not the same String Object.

If you then do:

String y = s;
String t = x;

You have a case where any string of the 4, .equals() any other string of
the 4 (s, t, x, y) will return true, but only s == y or t == x will
return true. That is because they refer to the same object.
 
J

john martin

if (s instanceof String) {
if (s == "a string");
if (s.equals("a string"));
}

what is the difference between s.equals and s == ???

When I use s == "something", many times if s == "something", returns false!

Instead s.equals works correct always.

Is there something that I dont know?

If you've ever worked with C or C++, it might actually be easier to
think of this in terms of pointers. Though Java doesn't have pointer
arithmetic, you can think of an object, such as the String s, as a
variable name (in this case s) pointing to the actual object. So, if
you compare two Strings as follows:

String s1 = new String("foo");
String s2 = new String("foo");

if(s1==s2)
System.out.println("equal");
else
System.out.println("not equal");

"not equal" will be the output, because s1 and s2 are not equal: they
point to different String objects.


If, however, you change it to:

String s1 = new String("foo");
String s2 = new String("foo");

if(s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal");

you'll get the expected output ("equal"), because the equals method is
overloaded to do a lexical comparison on the two String objects (in the
case of the String class, other objects can have their equals method
overloaded to do the appropriate comparison).

Often you'll want to overload the equals method for classes of your own
when you want to compare them to determine whether two objects should be
considered to have equal value regardless of whether they are in fact
pointing to the same instance. E.g., if you have a class with two
fields, and instances of that class can be considered equal when the
first field in the first object is the same as the first field in the
second object, and the second field in the first object is the same as
the second field in the second object, you'd override the equals method
to check for that condition and then return true if and only if that is
the case.

Hope that helps. I know it could be a somewhat confusing explanation
since it's often said that Java doesn't use pointers (and it's true that
it doesn't do pointer arithmetic at all). But it is often useful to
think of an object variable as a pointer to the object in memory (you
even have to use "new" to allocate the object; you can't just declare an
object on the stack the way you can a primitive such as int), even if
only for semantic purposes when thinking about the way the program will
work.
 
R

Ryan Stewart

john martin said:
Hope that helps. I know it could be a somewhat confusing explanation
since it's often said that Java doesn't use pointers

Who often says that?
 
L

Lewis Sellers

if (s instanceof String) {
if (s == "a string");
if (s.equals("a string"));
}

what is the difference between s.equals and s == ???

When I use s == "something", many times if s == "something", returns false!

Instead s.equals works correct always.

Is there something that I dont know?


"equals" physically goes and compares the contents of s and "something"
character by character.

"==" simply tests if the contents of s and "something" reside at the
same place in memory.... :-> See... and this is the tricky part... Part
of the way Java manages memory, objects, and strings in particular is to
try to have only one copy of a unique string in memory at a time. So if
you happen to mention the string "something" 15 times in your program,
Java will attempt to store it in one place in memory and just reference
that.

The main exception is when you do a new String.

--min
 
J

john martin

Ryan said:
Who often says that?

i've heard a number of people say that, although i don't think i've ever
seen anything i'd consider a "good" text on java say it (and i don't
think i've ever heard anybody reasonably knowledgable of java say it).
i've been asked in job interviews whether java uses pointers and it's
always apparent that "yes" is not the correct answer without some
explanation (e.g., "yes, but there's no pointer pointer arithmetic or
dereferencing" etc).
 
S

Scott Ellsworth

(e.g., "yes, but there's no pointer pointer arithmetic or
dereferencing" etc).

You don't get the job.[/QUOTE]

The one time I was asked that, I asked whether the interviewer meant
from a programmer's perspective, from a JVM perspective, or from a JIT
designer's perspective. (Not as off the wall a distinction as it sounds
- the company did a lot of JNI, and providing a better jitc was not
completely out of scope.) Apparently, there were plus points for asking
whether he considered a C++ reference as a pointer or not.

I got that job.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top