aspose file tools*
The moose likes Beginning Java and the fly likes difference between == and equal() in sting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "difference between == and equal() in sting" Watch "difference between == and equal() in sting" New topic
Author

difference between == and equal() in sting

subash ecm
Greenhorn

Joined: Apr 14, 2010
Posts: 1

difference between == and equal() in sting
Amit Vinod Dali
Ranch Hand

Joined: Apr 14, 2010
Posts: 42
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

Joined: Apr 10, 2010
Posts: 308
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:






Bye,
Nicola
saurabh agr
Ranch Hand

Joined: Apr 14, 2010
Posts: 37
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

Joined: Sep 04, 2004
Posts: 282

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.


Visit my blog! http://jameshambrick.com
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

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.


When you do things right, people won't be sure you've done anything at all.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Essential reading: Strings Literally


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between == and equal() in sting