File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Clone method showing different values for different programs. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clone method showing different values for different programs." Watch "Clone method showing different values for different programs." New topic
Author

Clone method showing different values for different programs.

VenkataPrasad Regula
Ranch Hand

Joined: Jun 30, 2011
Posts: 63
My task is to
1. Instantiate an object(a)
2. declare an other of the same class(b)
3. clone the object in step 1 to step 2(b=a.clone())
4. check if a and b are equal(a== b and a.equals(b))
5 Declare a hashset and add both a and b to the hashset.

Now my issues are as below
1. For Code 1 below HashSet is showing 1 as the count
2. For Code 2 below HashSet is showing 2 as the count.
Code1


Code 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3943
    
  17

VenkataPrasad Regula wrote:


You probably want to make the second assignment like y = b.hashcode(); Anyways, if you want your objects to behave properly when put into HashSets then you need to properly override the equals() and hashCode() methods. See this journal article for details: http://www.javaranch.com/journal/2002/10/equalhash.html


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clone method showing different values for different programs.
 
Similar Threads
java pass by value.
Finish my code please
hashCode and equals
Comparing two collections...
HashSet and equals()