• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

difference between == and equal() in sting

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

difference between == and equal() in sting
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String str1 = "subash";
String str2 = "ecm";
String combStr = str1 + str2;
String str3 = "subashecm";
System.out.println("combStr.equals(str3)? " + combStr.equals(str3));
System.out.println("combStr == str3? " + (combStr == str3));
--------------------------------------------------------------------------------------------
String's “equals()” compares the contents of the two Strings (i.e. the same sequence of characters). “==” is a fundamental operator which verifies whether two references are the same( i.e. whether references refer to same object)
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1 == s2: are s1 and s2 referencing the same String object?

s1.equals(s2): is the content of the String object referenced by s1 the same as the content of the String object referenced by s2 ?

Now, if you want, try this code:





 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as i know s1==s2 returns true if both string objects refer to same memory location and s1.equals(s2) returns true if the contents of both string objects are same.
 
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saurabh agr wrote:as i know s1==s2 returns true if both string objects refer to same memory location and s1.equals(s2) returns true if the contents of both string objects are same.



Yes I believe that's true. If s1 and s2 both point to the same object then it is equal using ==
.equals compares the value and tells whether it's equal.

To further clarify s1 points to a spot in memory, that spot has an address kinda like our homes do. So if the address is the same for both s1 and s2 then s1==s2 will return true. In C++ s1 and s2 will be called pointers since they point to an object that's in memory. In Java we don't refer to them as pointers.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also agree.

So....



This is similar when comparing Objects:


This is why you should never use the == operator when referring to objects... unless you're looking for the same object on the heap.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essential reading: Strings Literally
 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to collect some questions here in the forums to make a tutorial out for in my blog. I tried to answer your question here in this link.

http://devpinoy.org/blogs/lamia/archive/2010/04/15/the-difference-between-and-equals-when-used-in-strings-for-java.aspx
 
Marshal
Posts: 73244
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you didn't say in your blog is that the behaviour of Strings might vary depending on whether they are compile-time constants or not. It is a long time since I tried it, but you can have two identical Strings and have them return false to the == operator, but if they are made up using only compile-time constants, you might get true from ==. You will have to try it and go through the Java Language specification for more details.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic