Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Clone problem

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
we have to know cloning for scjp?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think Clone class is in the objectives...
 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if it is not in the objective ..could any one explane it...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as far as explanation goes, why do you expect to see true as the output?? You are calling super.clone in your clone implementation. The clone method of Object class will be called in that case. If you see the documentation of clone method in Object class, it says

While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.



Since you are not overriding the equals method, so when you call equals, equals method of Object class is called. The equals method of object class looks for reference equality i.e. return this == other. Since the references are not equal, so it doesn't matter age field has the same value in both objects. You will get true if you use this code

 
LOOK! OVER THERE! (yoink) your tiny ad is now my tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic