Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

equals method in String Class

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When i use String.equals() method with the argument of type StringBuffer with the same string as its content, the return value is false e.g.

String s = "Test";
String sBuf = "Test";

s.equals(sBuf) returns false.

Can anybody explain why?.

Regards
Abhijit.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhijit,

It returns "true" when I run your code. How did you run it?
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Try this,

public class Test{
public static void main(String[] args){
String s = "Test";
StringBuffer sBuf = new StringBuffer("Test");

System.out.println(s.equals(sBuf.toString()));
}
}
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



public class Test{
public static void main(String[] args){


String s2 = "Test";
StringBuffer sBuf = new StringBuffer("Test");

System.out.println(s2.equals(sBuf.toString()));
//Both are string objects with same content So Returns true
System.out.println(s2== sBuf.toString());

//There are 2 string objects So they refer to two different locations
// == checks for reference hence false

String s3 = "Test";
String sBuff = "Test";
//Here we are creating two string literals...For string literals intern() //function is implicitly called...So when sBuff is being created,there exist //already a literal..so it wont create another object in the pool siply //returns the reference to the object to sBuff.So only one object here

System.out.println( s3.equals(sBuff));

//Same as the first explanation So returns true

System.out.println( s3==sBuff);

//Here they are referring the same object..so returns true..

String s4=new String("Test");
String s5=new String("Test");


//Here we are creating 2 string objects..Implicit call of intern() doesnt //occurs for String object...created by new..So it creates 2 objects.


System.out.println( s4.equals(s5));

//Same as the first explanation So returns true

System.out.println( s4==s5);

//There are 2 string objects So they refer to two different locations //returns false


}
}






[ October 06, 2005: Message edited by: A Kumar ]
[ October 06, 2005: Message edited by: A Kumar ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,

That was a really good explanation. But can you tell me Why does it return false in this case?

StringBuffer sb1 = new StringBuffer("hi");
StringBuffer sb2 = new StringBuffer("hi");
if(sb1.equals(sb2)) // Why it returns false???

Can you please explain?

Thanks,
Usha.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the API docs, it says that the equals method is not overridden to match the concept of equality as in String.
Therefore two StringBuffer references are equal iff they point to the same object

StringBuffer sb1= new StringBuffer("hi");
StringBuffer sb2 = sb1;
sb1.equals(sb2) returns true, infact it is the same as '=='
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing
comparing a String and a StringBuffer using the equals will always return false because the equals method in String requires the argument passed also be of type String.
the only possible way to get a true using equals is when it is called in this manner
String1.equals(String2)
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Unlike String class,StringBuffer doesnt overrides the method
equals() of the Object class...

Here in StringBuffer

equals() methods checks the references of the objects ...and so they return false...

Hope You got It...

 
Usha Hanumolu
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Akshay...
-----------------------------------------------------------------------------
the only possible way to get a true using equals is when it is called in this manner
String1.equals(String2)
-----------------------------------------------------------------------------

This clears most of the confusion..

Thanks again,
Regards,
Usha.
 
Abhijit Sontakey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaanks all for the posts.
I decompiled the string class and found that for equals to return true, the Object passed must be of type String else 'false' is returned.
    Bookmark Topic Watch Topic
  • New Topic