Re: difference



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
.



Relevant Pages

  • another prob
    ... thanks a lot those who helped with the switch statements. ... The 7th step states (in the constructor of the car class pass the car model ... I would be very grateful if you could please help me. ...
    (comp.lang.java.help)
  • Re: Private Constructor
    ... > public static string Name ... > I intended to create an enumeration class similar to Color ... Car car = car.NISSAN; ...
    (microsoft.public.dotnet.languages.csharp)
  • Generics Serialization Exception
    ... Public Class Car { ... public NullableMeter; ... Car car = new Car; ... If I remove [XmlAttribute] then no problem it works. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Another simple problem
    ... > What if you wanted a method called newunder Foobar obj? ... > But then the Car() method call doesn't make sense. ... All I was proposing was changing the syntax of the constructor ...
    (comp.lang.java.programmer)