Linked Questions

1 vote
4 answers
1k views

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 ...
Razvan's user avatar
  • 10.2k
0 votes
2 answers
760 views

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 (...
lucare's user avatar
  • 35
379 votes
31 answers
469k views

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(); ...
olidev's user avatar
  • 20.8k
148 votes
4 answers
164k views

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) { ...
Dhananjay's user avatar
  • 4,005
24 votes
11 answers
7k views

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 ...
thomas8wp's user avatar
  • 2,421
12 votes
7 answers
5k views

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?
Saravanan's user avatar
  • 11.7k
15 votes
5 answers
13k views

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 ...
user2622016's user avatar
  • 6,513
3 votes
4 answers
12k views

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 ...
Anand's user avatar
  • 21.5k
17 votes
3 answers
821 views

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: ...
Dan's user avatar
  • 448
12 votes
3 answers
2k views

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 ...
Beamie's user avatar
  • 755
15 votes
2 answers
1k views

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 ...
Attila Csipak's user avatar
3 votes
6 answers
2k views

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 ...
user1613360's user avatar
  • 1,314
2 votes
4 answers
3k views

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 ...
daveb's user avatar
  • 3,535
4 votes
7 answers
645 views

I need to make equals function for MyClas. public class MyClass { boolean equals(Object value) { if (... value is type of MyCLass ...) { return= ... check conditions...; } ...
vico's user avatar
  • 18.5k
3 votes
4 answers
1k views

I have this @Override for equals() in my MyClass class: @Entity( name = "MyClass" ) @Table( name = "my_class" ) public class MyClass extends MySuperClass { ... @Override ...
Van's user avatar
  • 209

15 30 50 per page