• 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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone explain this result for me? Thanks in advance!
1. What is the output of the following
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
Ans:
a) false
false
false
dar
Why the second one is false also? according to book Cram p46,
the equals method compares content and takes an Object reference as input. what's wrong with this code ?
System.out.println(sb1.equals(sb2));
I don't understand. Please help!
YoungJa
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No rocket science here!. StringBuffer does not override the equals method, String does!
Hope you can figureout the rest

Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Young Ja!!
Even i also surprised to see the result, but later i went out and find out how equals() works in StringBuffer, it says that St.Buf class inherits its equals() method from object, so the method does not perform the expected behaviour and which wld be char-by- char comparison. this is what the explanation in R&H E book.
I guess it may help you.
all the best.
rajan
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I got it. but could you list the classes that override the equals method.
String and all Wrapper classes. what else?
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to Java API Documentation( If you don't have it, you can download it free from Sun's website) and switch to Index view. Look for equals(Object) method signature and you fill find a zillion of them.
Good luck,
Ajith
 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test36
{
test
{
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
}

public static void main(String args[])
{
Test36 t36 = new Test36();
t36.test();
}
}
I am trying to code an example to understand the above topic
i understood it theoretically, but unable to code an example
there are some errors in the above code, can someone explain
the errors in the above code
 
    Bookmark Topic Watch Topic
  • New Topic