• 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

equals()

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i m not getting the stuff "objects being meaningfully equal"
can some one tell m how equals() method works for objects & refernces
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider:



In this case, a.equals(b) is true because both strings contain "abc". This is what it means to be meaningfully equivalent. The data contained in one object is equal to the data in the other object. On the other hand, if you said: System.out.println(a == b), you would get false as the answer because the references are to different objects.

It all depends on how the equals() method is implemented in a class.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"objects being meaningfully equal".

In other words, their content is the same. The meaning of "the same" depends of the implementation of the equals() method. It has nothing to do with reference, so two different objects may be equal if you call the equals() method.

For example, a String will compare its content (characters) with the one passed as parameter.

Sorry for this not very clear explanation
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need to overwrite equals because you will check whether the state of two objects are the same instead of checking whether the two refernces points to the same memory location. The 'state' here means the value of instance variables. If instance variable of two objects have the same value then we say that two objects are equal. You need to overwrite by taking this. There are some other rules also to ovrride equals. For more information you can look into K&R book for Jaa 1.4 Chapter 6.

There will be a confusion between == and equals. By default(equals method written in Object class) the equals() method does the same thing what '==' does.

equals() use to find identical objects. and == use to find identical references.

This is a big topic. But hope this atleast clears your doubt a bit and make you think in a right direction.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I agree, this is a big topic and we should be careful because getting confused is very easy.
So it's important to differentiate reference variables and comparing objects. We use The == operator to compare two references variables to see if the two refer to the same object. x==y returns true if and only if x and y refer to the same object.
We use the equals() method to compare the contents of two objects whenever possible; equals() is used only to compare objects.
Talking about "meaningfully equivalent" is another subject, for example:
Are lo and in meaninfully equivalent?

Enrique Villamizar
P.S. English isn�t my first language.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by zaheer agadi:
...i m not getting the stuff "objects being meaningfully equal"
can some one tell m how equals() method works for objects & refernces


Consider a simple class called Car. What does it mean for two instances of Car to be equal?

In some situations, maybe all we care about is the make and model. So 2 instances of Volkswagen Jettas would be considered "equal" even if they were different years of production, had different engines, etc. In this case, we would define the equals method to look only at these 2 fields (make and model).

In other situations, we might want to go deeper, and say that two instances of Car are equal only if make, model, production year, engine type, and color are the same. So in this case, we would define the equals method to look at 4 fields: make, model, year, and color.

So a "meaningful" definition of equals depends entirely on how you define the method. That is, what is meaningful to you.

In contrast, the simple value comparison (==) returns true only if the references are the same (point to the same object).
 
zaheer agadi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx a lot, u guys cleared my doubt
 
reply
    Bookmark Topic Watch Topic
  • New Topic