• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compare object

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://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.



 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to override equals?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)


 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, You mean that equal method in array class, overrides equal method in object class?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then what is the relationship between them?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic