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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

doubt in == for a string operation

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Report post to moderator
in the main function

public static void main(String args[])
{
String a="hai";
String b="hai";
String c=new String("ram");
String d=new String("ram");
System.out.println(a==b);
System.out.println(c==d);
}

output:

true
false

Why is c==d is returend as false ??
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Report post to moderator
== operator is used only for address comparision. not for the
content comparision. here c and d are objects having some addresses.

coming to equals method .. it is also for address comparision. if any class
is overridden that method from object class... that implementation is specific to that class. for example String class... it
overrides the equals method. in this case this method for content comparision only.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Report post to moderator
Duplicate post!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please don't create multiple topics with exactly the same question.
Continue in your other topic.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!