• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

difference between "equal()" and "==" in java.

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
difference between "equal()" and "==" in java.
why java have provided a seperate method "equal()" to compare if can do the same with "==" only.

Kindly explain me in brief with some e.g.

thanks in advance......
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:
why java have provided a seperate method "equal()" to compare if can do the same with "==" only.



Because they are not the same thing. The "==" operator is used detect whether two references are pointing the the exact same object. The equals() method is class specific -- for example, strings are equal when the value is equal, even if it is two different string objects.

Henry
 
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you have something like

MyClass objectA, object B

what you have are references to the objects. You can think of them as like little cards that tell you the address of a house. using '==' tells you if both pieces of paper say "1600 Pensylvania avenue". using equals() might tell you if both homes are worth $150,000, or if both have three bedrooms, or if both are exactly the same square footage...

equals() can be defined to be whatever 'makes sense' for that object type.
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is 1600 Pennsylvania Ave really worth as much as $150000?
 
It will give me the powers of the gods. Not bad for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic