aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcodes" Watch "hashcodes" New topic
Author

hashcodes

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
this is an eg from K&b quiz..here after running the code i get same answer even if i keep hashcode method or delete hashcode method...but the answer given there is that if hashcode method is overridden then duplicate values do not get printed and my answer should be 3 instead of 4???
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

meghs karve wrote:...but the answer given there is that if hashcode method is overridden then duplicate values do not get printed and my answer should be 3 instead of 4???

Yes thats right, you're adding the same element having value "34" 2 times, but as it IS A Set, the object didn't accept the second element with value 34.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
copy my code as it is and run and c the output.....u will understand what i am trying to say...............


thanks ..............
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
replace this


with this

and run the code, I hope you will get your answer.


SCJP 1.6 96%
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

This one is very tricky. It should be:



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
that means i cant sort and remove duplicates in case if i put a Class in generics....It will only work for wrapper class objects???not user defined classes???
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

meghs karve wrote:that means i cant sort and remove duplicates in case if i put a Class in generics....It will only work for wrapper class objects???not user defined classes???

Just ignore what Neha Daga said. Of course you can use collections classed with your own classes. Just implement the hashCode and equals methods correctly.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ya right....i expected to get an output of 3 but instead getting 4...strange....still cant figure out why....


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
what I meant to say is you are creatint a new object everytime doesn't matter what it contains is equal or not.

@Wouter Oet
I wanted him to figure out his mistake
If he had realised what I wrote above, He would have been able to figure out what was wrong in his hashCode method.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Raju Champaklal wrote:ya right....i expected to get an output of 3 but instead getting 4...strange....still cant figure out why....

Have you read the topic? I just posted the solution
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Set cant take equal objects....one is a duplicate....
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
oh didnt see the small c
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Neha Daga wrote:what I meant to say is you are creatint a new object everytime doesn't matter what it contains is equal or not.

NO it does matter what is contains. The equals method should return true is 2 objects are meaningful equal.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I have edited my message please read.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ignore what Wouter has said....add method will give false if it sees that if two objects are meaningful equal
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Your intentions where good although I find the advice vague. Maybe it's the language barrier.

My advice: use the @Override annotation. Then you'll get an compile-time error when your
not overriding something. What just happened.

Raju Champaklal wrote:ignore what Wouter has said....add method will give false if it sees that if two objects are meaningful equal

But in the add method hashCode and equals are used. So if you don't implement the hashCode and equals methods properly the method
will return true even though the set already contains an meaningful equal object.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ignore what Wouter has said....dont use the @Override annotation...dont use netbeans...or eclipse...now you know that there is a capital C in the hashCode method
just use simple editors like editplus or notepad
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
meaningful equal objects mean the equals and the haashCode method has been overridden.....
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why not use the override annotation? It's part of the java language! I agree with you that you shouldn't use ide's. I write all my code in gedit.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Raju Champaklal wrote:meaningful equal objects mean the equals and the haashCode method has been overridden.....

So if i have new Integer(5) and new Integer(6) they are meaningful equal because the hashCode and the equals methods have been overriden??
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
if you want to study for scjp dont use IDEs....but if you are working on some project definitely go for IDEs....they make your work faster...but if you dont understand why it is getting faster using an IDE wont help
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
meaningful equals object mean new Integer(5) and new Integer(5).....
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I agree but you haven't answered my question. Why not use the java.lang.Override annotation?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Raju Champaklal wrote:meaningful equals object mean new Integer(5) and new Integer(5).....

Yes but then is not what you said here:

Raju Champaklal wrote:meaningful equal objects mean the equals and the haashCode method has been overridden.....
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
whatever i mean here is only while preparing for scjp ....
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
new Turtle(8) and new Turtle(8) are not meaningful equal if you dont override equals and hashCode.....two object are equal if they are the same...two objects are meaningful equal if they are not the same object but equal according to the hashCode and equals method that have been overridden

i hope you are clear now
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That I understand. That is why I'm here. I'm currently preparing for SCJP. However you have still not giving me a reasoned explanation about why not to use the java.lang.Override annotation.
It's part of the java language. It's not bound to a certain compiler or IDE.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
look if you use that notation then the compiler will help you find the error....this is good if you are building a project and you want to find the trivial errors fast....then i totally agree use that notation....but while preparing for scjp i think the better you get at finding out the errors the better...if i hadnt seen this question today i wouldnt have ever found that c in hashCode is capital...now in future i would make sure that C is capital....
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Then we agree to disagree. I would use the annotation even while preparing for SCJP. The compiler error that you can't override hashcode (no capital c) sticks with you .
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
i was just giving a suggestion....and please never ask someone to ignore someone else's advice...
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Maybe that was not very nice. However then you shouldn't do it too. His post was not very helping.
Instead of helping to solve the problem he suggest a alternative which is in most cases not the best answer and certainly not in this case.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
because i was making you taste your own medicine...it doesnt look nice....sorry..
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
@Wouter Oet

I am not a he I am a She.
Phungsuk Wangdu
Ranch Hand

Joined: Nov 04, 2009
Posts: 114
Wouter
neha daga is a she ...
She has become red already.....hhehehehe with anger
well guys i am just joking hope all people will be cool..
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I am really sorry
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
its fine man...no hard feelings
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10221
    
166

Be nice everyone

Raju Champaklal wrote:look if you use that notation then the compiler will help you find the error....this is good if you are building a project and you want to find the trivial errors fast....then i totally agree use that notation....but while preparing for scjp i think the better you get at finding out the errors the better...if i hadnt seen this question today i wouldnt have ever found that c in hashCode is capital...now in future i would make sure that C is capital....


The @Override is there for a reason:

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.


It is highly recommended that the @Override annotation be used when you are overriding the declaration or implementing the method from an interface (java 6).

In the example, being discussed here, the intention is to override the implementation of hashCode(). Irrespective of whether the example is for SCJP or for any project, it is right to use the @Override.


[My Blog] [JavaRanch Journal]
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcodes