Re: difference
- From: "Jim McMaster" <jim.mcmaster@xxxxxxxxx>
- Date: Wed, 08 Feb 2006 02:42:42 +0000
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?
--
.
- Follow-Ups:
- Re: difference
- From: Bonney Armstrong
- Re: difference
- References:
- difference
- From: venkatrao jampani
- difference
- Prev by Date: Re: how to feed a Process object keyboard input
- Next by Date: Re: New Java concurrency package: samples?
- Previous by thread: Re: difference
- Next by thread: Re: difference
- Index(es):
Relevant Pages
|
|