Re: difference



On 2/7/2006, at 6:56:54 AM, venkatrao jampani wrote:

what is the difference between == and isequals in java?

Every object has an equals() (not isequals()) method. The default
method, inherited from Object, just checks to see if the two objects
being compared are the same object. Subclasses of Object can override
equals() to implement more specific behaviour when required.

== is a java language operator that compares two primitive values for
equality. It also compares object references, and gives the same
result as Object.equals(). Consider the following code:

Integer a = new Integer(1);
Integer b = new Integer(1);

System.out.println(a == a);
---> true, same object reference

System.out.println(a == b);
---> false, different object references. This would probably not be
what you would expect in your code.

System.out.println(a.equals(b));
---> true, uses Integer.equals() to override the default behaviour so
any two Integers with the same value are considered equal, even if not
the same object.

If you are comparing primitives, like int, you have no choice but the
== operator. For example:

int c = 1;
int d = 1;

System.out.println(c == d);
---> true, same value

System.out.println(c.equals(d));
---> Won't even compile. You get the error "Cannot invoke equals(int)
on the primitive type int".

Make sense?


--

.



Relevant Pages

  • Re: Recommended style
    ... >> There's no conversion to int. ... >> comparison to a null pointer constant. ... It's of type int. ... its operand compares unequal to 0, 1 if the value of its operand ...
    (comp.lang.c)
  • blanking a java.util.Date in 1.5
    ... I have some code that compares java.util.Date. ... int dateInt = 25; ... * Clears everything from the calendar arg. ... public static void clearCalendar(Calendar calendar) ...
    (comp.lang.java.programmer)
  • Re: confused..again
    ... The code above compares the addresses of name and name2, ... You pick up little things like int c is ... However, this is not a support group, so don't expect most people to understand your problems or be supportive of them. ...
    (comp.lang.c)
  • Re: equals vs ==
    ... == compares identity, equalscompares equality. ... references referring to it. ... equals() will return also true if the ... String overrides it by comparing the contents of the string. ...
    (comp.lang.java.programmer)
  • Re: InetAddress.equals() Anomoly?
    ... Compares this object against the specified object. ... array components is the same for the byte arrays. ... > If you consider that two InetAddress should be equal as soon as they have ... If you start overriding equals ...
    (microsoft.public.dotnet.vjsharp)