Bookmark Topic Watch Topic
  • New Topic

cloan problem  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Report post to moderator
quote: class A implements Cloneable
{
int age;
A(int age)
{
this.age=age;
}
public int getage()
{
return age;
}
public Object clone()
{
A aa=null;
try
{
aa=(A)super.clone();

}
catch(Exception e)
{

}
finally
{
return aa;
}
}
public static void main(String str[])
{

A a1=new A(10);
A a2=(A)a1.clone();
System.out.println(a1.getage()+"==="+a2.getage());
System.out.println(a1.equals(a2));

}
}



output :

C:\>javac A.java
C:\>java A
10===10
false

expected output:

C:\>javac A.java
C:\>java A
10===10
true
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Report post to moderator
Why do you expect the output of a1.equals(a2) to be true ? The Javadoc for the Object.equals() method explains why it returns false.
[ October 31, 2008: Message edited by: Joanne Neal ]
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
please don't make duplicate posts. it wastes everyone's time. follow-ups should be made here.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!