This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compare object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare object" Watch "compare object" New topic
Author

compare object

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 13, 2005
Posts: 37936
    
  22
What do you understand from the API documentation for equals() and the Comparable interface?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
How to override equals?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Sep 06, 2007
Posts: 635


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

Joined: Sep 20, 2010
Posts: 3575
    
  14

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Sep 20, 2010
Posts: 3575
    
  14

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Oct 25, 2008
Posts: 2700

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.


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

Joined: Sep 06, 2007
Posts: 635
Well, You mean that equal method in array class, overrides equal method in object class?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No. It is a static method so it can't override anything and the argument list is totally different.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Then what is the relationship between them?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare object
 
Similar Threads
Java Vs .Net
q on wrapper classes
*.MAX_VALUE
Confusing outputs
Wats the O/P...