Re: difference
- From: Fabien Bergeret <fabien.bergeret@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 07 Feb 2006 15:22:34 +0100
venkatrao jampani wrote:
what is the difference between == and isequals in java?I think you mean == ans equals ?
== tests the equaliy of reference (both the objects have the same address), and equals tests the equality of value.
For instance, you may say that 2 cars having the same Vehicle Identification Number are the same :
public class Car {
private long vin;
public boolean equals(Object o) {
if ( o instance of Car ) {
Car paramCar = (Car)o;
return getVin() == paramCar.getVin();
}
else
return false;
}
...
}
now :
Car c1 = new Car(123);
Car c2 = new Car(123);
Car c3 = c1;
Car c4 = new Car(454);
boolean t1 = (c1 == c3); //true : both c1 and c3 have the same address in memory
boolean t2 = c1.equals(c3); //true : both c1 and c3 have the same vin
boolean t3 = (c1 == c2); //false : c1 and c2 have different addresses
boolean t4 = c1.equals(c2); //true : both have the same vin
boolean t5 = c1.equals(c4); //false : different vin
.
- Follow-Ups:
- Re: difference
- From: Gordon Beaton
- Re: difference
- References:
- difference
- From: venkatrao jampani
- difference
- Prev by Date: difference
- Next by Date: Re: difference
- Previous by thread: difference
- Next by thread: Re: difference
- Index(es):
Relevant Pages
|