hello everyone, i was going through exercise in book and here is the part i need some help.
Consider this code that creates some location objects with coordinates x=10 and y=20:
Location a, b, c;
a=new Location (10,20);
b=new Location (10,20);
c=b;
after this code executes, what are the values of these boolean expressions?
A= =b
a.equals(b)
a= =c
a.equals(c)
b= =c
b.equals(c)
Comments
:
: Consider this code that creates some location objects with coordinates x=10 and y=20:
:
: Location a, b, c;
: a=new Location (10,20);//creates an object
: b=new Location (10,20);//creates an object with the same values of a, but is not the same object
: c=b;//c now refers to b ie they point to the same location in memory
:
: after this code executes, what are the values of these boolean expressions?
//the boolean expressions refer to memory addresses, not the value of the object
//equals() evaluates some value in both objects, not the references. What they return, depends on the implementation of Location
:
: A= =b
: a.equals(b)
: a= =c
: a.equals(c)
: b= =c
: b.equals(c)
:
You can compile this code(after some modifications of course) and find out, but in short it is helping to point out that you rarely want to compare 2 objects with ==. That 2 references refer to the same object is rarely useful info. But, how they compare in relation to the object value has many important applications.
[italic][blue]Just my 2 bits[/blue][italic]