Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

== and equals in String Class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
can someone let me know how == and equals work
in the following cases ?
String a1= new String("a")
String a2= new String("a")
Stirng a3=String("a")
String a4=String("a")
Stirng a5="a"
String a6="a"
a1==a2
a1.equals(a2)
a3=a4
a3equals(a4)
a5==a6
a5.equals(a6)
a1==a3
a1.equals(a3)
a1=a5
a1equals(a5)
Will the ans remain same if all the above were
StringBufferclasses instead of string objects ?
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == operator compares two variables to determine if they contain the same value. In the case of reference variables, that means that the == operator will determine if each variable contains the same value. That will only return true when both variables contain a reference to the same object.
However, if a class, such as String, overrides the equals method, you can call that method to determine if two objects contain the same values, not to reference variables.
Check out the JLS for more information:
§15.21.3 Reference Equality Operators == and !=
Corey
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a trick though with Strings. When a String is allocated with a new:
String s = new String("ABC");
then all the regular rules apply because the String is allocated on the heap. So you will get this behavior:
String a = new String("ABC");
String b = new String("ABC");
System.out.println(a == b) // prints false
However if you declare a String using = instead of new:
String c = "ABC";
the behavior is different because this String is allocated in the literal pool. So you get this behavior:
String c = "ABC";
String d = "ABC";
System.out.println(c == d) // prints true
of course, using .equals() method will find a, b, c, and d all equal.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to continue on Thomas' trend -- as a general rule of thumb, always use String abc="abc" rather than the new operator. So you can take advantage of the String Literal Pool.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!