• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compare object

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean comparison of object? If all the variable equal?

There is two method for this equal and compare, Which one is better? Or when to use one of them?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you understand from the API documentation for equals() and the Comparable interface?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/517432/java/java/equals-method-Java#2342352

OK I read something, But I don't know why that compare return False? Henry explained, But I need to know more.



 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object.equals is implemented to use only ==. You did not override equals. That means that both E.equals and F.equals only use ==. As such, e.equals(f) is the same as e == f.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to override equals?
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to override any method? Also, SearchFirst to find many topics which have enough hints in them on how to override it properly. However it's implemented, it will involve either using instanceof, comparing classes or only ==.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at equals.E.equals(E.java:14)
at equals.Main.main(Main.java:16)

 
Stephan van Hulst
Bartender
Pie
Posts: 5807
61
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your equals method return when

this != obj, and
obj instanceof F?

Why are you comparing an E to an F by the way?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you comparing an E to an F by the way?

I wanted to learn how equal can used to compare two objects.
 
Stephan van Hulst
Bartender
Pie
Posts: 5807
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The equals method is meant to compare two objects of the defining class' type, or its subtype.

Since you're defining equals in the E class, you should only compare an E to an F if F is a subclass of E, which it's not.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can define a method by the name abalfazl that can compare objects like this:




Why equals is used for this purpose?

another question:



equals works without override.How?Why for comparison two object needs to override
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:I can define a method by the name abalfazl that can compare objects like this:

No you can't because it isn't a valid override nor overload.

Comment about your code:


abalfazl hossein wrote:Why equals is used for this purpose?

Search for it. This question has been answered millions of times.

abalfazl hossein wrote:equals works without override.How?Why for comparison two object needs to override

Arrays.equals is just a method. Nothing more nothing less. If you override the equals method then it can be used with polymorphism. Otherwise it can't.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, You mean that equal method in array class, overrides equal method in object class?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. It is a static method so it can't override anything and the argument list is totally different.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what is the relationship between them?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name. The equals method of object isn't a special method. It's just a method with returns a boolean. The only special thing about it is that it is used in Collections to determine if the object is equal to another object. Arrays.equals() takes to arrays and compares them and returns if they are equal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic