Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between == and equal() in sting

 
subash ecm
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

difference between == and equal() in sting
 
Amit Vinod Dali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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)
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • 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:





 
saurabh agr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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.
 
James Hambrick
Ranch Hand
Posts: 282
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essential reading: Strings Literally
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic