Help coderanch get a
new server
by contributing to the fundraiser
  • 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 and ==

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when to use equals() and == for comparison of string objects.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the == operator checks whether two reference variable refer to the same object.that is whether two variable refer to the same bit pattern .whereas the equals operator checks for the content
Eg
1

String a=new String("Java");
String b=a;
if we say
a==b than
this will return true
Eg 2
String a=new String("java") ;
String b=new String("java");
now when we say
a==b
this will return false
But for the same if we say
a.equals(b)
it will return true
=============
key point to remember == can be used to compare variable refering to the same bit pattern
and equals is used to check the content of a reference variable
you cannot use equals for two different object
it will always return false if we use it for two different objects.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The short answers is you should never use == to compare String objects, or ANY objects, for that matter.
 
Do you want ants? Because that's how you get ants. And a tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic