Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

the instance with new apply to String and StringBuffer are different?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read the code first,why the sb1.equals(sb2) return a false valeu?
/*******************************************************//
public class TestClass
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
String sb3 = new String("String");
String sb4 = new String("String");

if(sb1.equals(sb2))
{
System.out.println(" StringBuffer:I am reache here!");
}
else
System.out.println("StringBuffer:sorry!");
if(sb3.equals(sb4))
{
System.out.println(" String:I am reache here!");
}
else
System.out.println("String:sorry!");
}
}
//*******************************************************//
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sb1, sb2 are StringBuffer Objects.
StringBuffer class does not override equals() of Object class
In such a case, the equals() performs the same role as ==; i.e it will check for reference equalities.
In our case sb1, sb2 are two different objects and they point to different references.
Hence the result.
HTH
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no equals method for StringBuffer.
Ji

Originally posted by james gong:
read the code first,why the sb1.equals(sb2) return a false valeu?
/*******************************************************//
public class TestClass
{
public static void main(String Args[])
{
StringBuffer sb1 = new StringBuffer("String");
StringBuffer sb2 = new StringBuffer("String");
String sb3 = new String("String");
String sb4 = new String("String");

if(sb1.equals(sb2))
{
System.out.println(" StringBuffer:I am reache here!");
}
else
System.out.println("StringBuffer:sorry!");
if(sb3.equals(sb4))
{
System.out.println(" String:I am reache here!");
}
else
System.out.println("String:sorry!");
}
}
//*******************************************************//


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ji,
Every class has the <code>equals()</code> method. They inherit it from <code>java.lang.Object</code>.
The <code>StringBuffer</code> class doesn't override it while <code>String</code> and the wrapper classes do.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
    Bookmark Topic Watch Topic
  • New Topic