• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hi

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class MyClass
{
int maxElements;
MyClass()
{
System.out.println("true");
}

MyClass(int maxElements)
{
this.maxElements = maxElements;
}

}

public class Q19 extends MyClass
{
public static void main(String[] args)
{

MyClass a = new MyClass(100);
MyClass b = new MyClass(100);

if(a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("Objects have different values");
}
}
output bjects have different values
my expected output bjects have same values
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani,
When you call the equals() method, you're actually calling the equals() method from class Object. You need to override this method then your code should be ok.
Here's an example:

Hungson Le
[This message has been edited by Son Le (edited January 24, 2001).]
[This message has been edited by Son Le (edited January 24, 2001).]
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I did a little modify on your program. Nothing just assign
your b reference value into a then i got the result of
the "Objects have the same values". Look, you make the
different memory location for a and for b reference. Both a
and b are not in the same memory location whether object name
is the same i.e. MyClass.Look if you use the following statement
it would give the same effect.
myClass a= new myClass(100);
myClass b= a; // Because you are assigning
the copy of referece a to b
Whether object is same.
Also your Constructor a and b are replacing default
constructor of Myclass i.e. without parameters constructor.
It is no relationship with Constructor having int parameters.
class myClass{
myClass(){}
int maxElements;
myClass( int maxElements){
maxElements=maxElements;
}
}
public class ChildClass{
public static void main( String argv[] ) {
myClass a= new myClass(100);
myClass b= new myClass();
a=b;
if (a.equals(b))
System.out.println("Objects have the same values");
else
System.out.println("This is the different values");
}
}
 
rajani adapa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u guys sanle and golam,
but can u tell me the reason y my code is not working
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The things you are looking for, you will get it at the following
example.
1. Given the following classes defined in separate files:
If you run this program, you would get answer n. 3 :
Vehicle drive.
Car drive.
Car drive.

As same i gave you the before example.
class Vehicle {
public void drive() {
System.out.println("Vehicle: drive");
}
}

class Car extends Vehicle {
public void drive() {
System.out.println("Car: drive");
}
}

public class Test {
public static void main (String args []) {
Vehicle v;
Car c;
v = new Vehicle();
c = new Car();
v.drive();
c.drive();
v = c;
v.drive();
}
}

What will be the effect of compiling and running this class Test?
1.Generates a Compiler error on the statement v= c;
2. Generates runtime error on the statement v= c;
3. Prints out:
Vehicle: drive
Car: drive
Car: drive
4. Prints out:
Vehicle: drive
Car: drive
Vehicle: drive
Select the most appropriate answer.
Thanks,
Golam Sayeed
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello rajani adapa
U r getting the output as different because u r making two obejcts of the same class which are two different reference
so when u will
compare they will refer to different address locations.
That's why the output is different values
But as u see the Goalm exm. When u assign one object reference to another object refernce then both becomes alias and refer to same address location.
so if u will do a = b or b = a then u 'll get the output as sames values
Hope this clears ur doubt.
Thanx
 
rajani adapa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
o.k.
Then how come this results true.
Double d1=new Double(12.0);
Double d2=new Double(12.0);
and d1.equals(d2)=true;
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Rajani
The reason the answer is "true" is because you are :
1. Comparing two Objects belonging to the same wrapper class
i.e both are Double's
2. And both these Objects contain the same value 12.0.
When comparing two Objects with equals, Java first ensures that you are comparing Objects of the same type, if that is true then it verifies the actual contents in the objects.
Hope this helps.
Latha
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani,
Short answer:
In your first example you are calling the Object equals method (MyClass doesn't override equals). It checks to see if both references are pointing to same memory location. Yours are not because you created two separate objects.
In your Double you are calling the Double equals method which has overridden the Object one. It checks to see if the value contained in each object is the same. Yours are: both 12.0!
Regards,
Manfred.
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it is very tricky question to me.
1. Note that in most cases, for two instances of class Double,
d1 and d2, the value of d1.equals(d2) is true
if and only if
d1.doubleValue() == d2.doubleValue()

also has the value true. However, there are two exceptions:
If d1 and d2 both represent Double.NaN, then the equals
method returns true, even though
Double.NaN==Double.NaN has the value false.

If d1 represents +0.0 while d2 represents -0.0, or vice versa,
the equal test has the value false,even though +0.0==-0.0 has
the value true. This allows hashtables to operate properly.
2. Double is a final type class object. So as per jdk1.2 , final
classes can't Sub classes, in where your myClass is not final
so it can be Subclass i.e. you can extend your myClass. Kindly
check your program changing like final class myClass{} and
make two instance in another class, it would give "objects
have the same values" without assigning a copy of reference.
3. Your Class is not primitive type Object class, where Double,
Integer, Float are primitve double, int , float type Object
class.
I hope it would be hlpeful for you,
Thanks,
Golam Sayeed Newaz
 
rajani adapa
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all for a great explanation
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I came up again with nice example about equal problem.
Kindly notice display which is the output of TestEqual.java.
QUOTE:
public class TestEqual
{
public static void main (String[] args) {
// Long and Integer classes has overided the equals method
Long l1 = new Long(7);
Long l2 = new Long(7);

if (l1 == l2)
System.out.println("l1 == l2");
else
System.out.println("l1 != l2"); // display

if (l1.equals(l2))
System.out.println("l1.equals(l2)"); // display
else
System.out.println("NOT l1.equals(l2)");

Integer i = new Integer(7);

// Not compilable
// Incompatible type for java.lang.Integer
// cannot convert Integer to Long
//if (i == l1)
// System.out.println("i == l1");

// Using the equals method on a diff type always produces
// false, regardless of content
if (i.equals(l1))
System.out.println("i.equals(l1)");
else
System.out.println("NOT i.equals(l1)");// display


// String class has overrided the equals method
String s1 = new String("700");
String s2 = "700";
String s3 = "700";
String s4 = new String("700");

// different object references
if (s4 == s1)
System.out.println("s4 == s1");
else
System.out.println("s4 != s1");// display

if (s4.equals(s1))
System.out.println("s4.equals(s1)");// display
else
System.out.println("NOT s4.equals(s1)");

// s2 and s3 are actually pointing to the same literal
if (s3 == s2)
System.out.println("s3 == s2");// display
else
System.out.println("s3 != s2");

if (s1.equals(s2))
System.out.println("s1.equals(s2)");// display
else
System.out.println("NOT s1.equals(s2)");


// User defined class extends Objects
// Override equals methods will make a difference
NotOverrideEquals noe1 = new NotOverrideEquals(3);
NotOverrideEquals noe2 = new NotOverrideEquals(3);

// default implementation is compare the obj reference
if (noe1.equals(noe2))
System.out.println("noe1.equals(noe2)");
else
System.out.println("NOT noe1.equals(noe2)");// display


OverrideEquals oe1 = new OverrideEquals(3);
OverrideEquals oe2 = new OverrideEquals(3);

// the default behavior is overrided
if (oe1.equals(oe2))
System.out.println("oe1.equals(oe2)"); // display
else
System.out.println("NOT oe1.equals(oe2)");
}
}
class NotOverrideEquals {
int nn;
NotOverrideEquals(int n) {nn = n;}
}
class OverrideEquals {
int nn;
OverrideEquals(int n) {nn = n;}

// override the Object equal method
public boolean equals(Object obj) {
if ( !(obj instanceof OverrideEquals) )
return false;

OverrideEquals o = (OverrideEquals)obj;
return (nn == o.nn);
}
}
UNQUOTE:
Thanks,
Golam Sayeed Newaz
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic