Linked Questions
34 questions linked to/from Any reason to prefer getClass() over instanceof when generating .equals()?
1 vote
4 answers
1k views
Wrong implementation pattern for Java equals method [duplicate]
I've seen many implementations of the java equals() method which go along the following lines: public boolean equals(Object other){ if (this == other) return true; //this if code ...
0 votes
2 answers
760 views
How to rewrite the equals method [duplicate]
I saw a colleague write it as follows: @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (...
379 votes
31 answers
469k views
Is it possible to use the instanceof operator in a switch statement?
I have a question of using switch case for instanceof object: For example: my problem can be reproduced in Java as following- if(this instanceof A) doA(); else if(this instanceof B) doB(); ...
148 votes
4 answers
164k views
instanceof Vs getClass( )
I see gain in performance when using getClass() and == operator over instanceOf operator. Object str = new Integer("2000"); long starttime = System.nanoTime(); if (str instanceof String) { ...
24 votes
11 answers
7k views
Should 2 Java objects of different classes ever be equal?
I'm trying to write some generic code to define class equality and hashcodes based on a list of fields. When writing my equals method, I was wondering if, based on Java convention, it should ever be ...
12 votes
7 answers
5k views
Do we need the method getClass() from java.lang.Object?
I know getClass() method in java.lang.Object. But In my application, I know all of my classes. So, What is the actual purpose of using this method?
15 votes
5 answers
13k views
Java .equals() instanceof subclass? Why not call superclass equals instead of making it final?
It is stated in Object's .equals(Object) javadoc: It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. Almost ...
3 votes
4 answers
12k views
Java objects are equal though hashcode is different
While reading about equals() and hashcode(), I came to know that if two objects are equal, then their hashcodes must be equal, though not vice-versa. But below example doesn't reflect this. class ...
17 votes
3 answers
821 views
Which part of the equals() general contract does my equals() not satisfy
I'm fairly new to java and am just trying to get my head around understanding @Override of the equals() and hashcode() methods. I know for the equals method to be correct it needs to be: Reflexive: ...
12 votes
3 answers
2k views
How to implement equals with hibernate without risking losing the symmetric property?
After reading up on (again, should have done this a long time ago) implementing equals and hashcode correctly i came to these conclusions,that works for me: If pre JDK 7: Prefer using Apache commons ...
15 votes
2 answers
1k views
Why does the equals() implementation generated by Eclipse check for null before type checking (instanceof)?
I regularly used Eclipse's code generation tools (Source / Generate hashCode() and equals()...) to create the equals() implementation for simple POJO classes. If I choose to "Use instanceof to compare ...
3 votes
6 answers
2k views
Java compareTo() method returns classCastException
Let's assume I have an Employee base class and Manager subclass which extends Employee.Now let's say I create an object x of type Employee and object y of type Manager and call x.compareTo(y) no ...
2 votes
4 answers
3k views
Override equals and hashCode methods in superclass or in the sub class or override in both
I am relatively new to java programming and have had a problem finding out where to use equals and hashcode method overriding when I have a subclass inheriting from the superclass. I want to check ...
4 votes
7 answers
645 views
is Object instance of MyClass
I need to make equals function for MyClas. public class MyClass { boolean equals(Object value) { if (... value is type of MyCLass ...) { return= ... check conditions...; } ...
3 votes
4 answers
1k views
Java @Override equals(): When this.getClass() != o.getClass() fails but shouldn't
I have this @Override for equals() in my MyClass class: @Entity( name = "MyClass" ) @Table( name = "my_class" ) public class MyClass extends MySuperClass { ... @Override ...