• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

about string and string buffer

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone explain the difference between equal() and "==".
thanks in advance
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equal() does a deep comparison. ie it compares the contents.
== does a shallow comparision i.e it just test to see if the both the operands have a same reference.
While creating strings (say u want s="hello" ), in java strings are immutable, so the VM searches to see if there is a "hello" string already. if there is one, it assigns that address to the variable s.
now while comparison say u want to compare s with a(some other varible), then s.equal(a) compares the actual contents where as s==a just compares the reference.
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rgowka1:
equal() does a deep comparison. ie it compares the contents.
== does a shallow comparision i.e it just test to see if the both the operands have a same reference.
While creating strings (say u want s="hello" ), in java strings are immutable, so the VM searches to see if there is a "hello" string already. if there is one, it assigns that address to the variable s.
now while comparison say u want to compare s with a(some other varible), then s.equal(a) compares the actual contents where as s==a just compares the reference.


Careful, == compares the contents of the variable or reference. In the case of a refernce, this is the memory location of an object so, it tests to see if the references refer to the same object.
equals() is a method defined in Object which basically does the same thing, checks to see if the refernces refer to the same object. However, in some of the APIs including String class, equals is overridden to compare the contents of the reference and not just to see if it is the same object. You can in any of your classes override equals to do what is appropriate for your class.
So, check the documentations for any class (or super class) that you are using from the API's to see what the effect equals() will have.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic