• 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

Whant is 'equals' and '==' in JAVA mean......?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody help me in explaining what do we mean by following operators in JAVA:

'equals' and '=='

Please give a brief definition about how can i use them and try to give a simple example
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Object class defines a method called equals that is inherited by all classes.

This method allows us to define what it means for two different objects to be meaningfully equal.

However, if a class does not override equals, then the default behavior is simply to check to see if the object reference calling the method and the object reference sent to the method refer to the same object.

Consider the following example.



first and second refer to different objects, but they are meaningfully equal.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More about what Keith Lynn says. If you are dealing with a primitive type, you can only use the == operator.
Whenever you are trying to use an equals() method, go to the API for that class and look for its equals method. If it appears in the block headed "mothods inherited from java.lang.Object" then it hasn't been overridden and does what Keith Lynn calls default behaviour.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic