• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

stringbuffer.equals method

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is why the following statement: System.out.println(stringbuffer.equals(string));

returns false (assuming stringbuffer and string are valid StringBuffer and String objects respectively). Instead, shouldn't it give a compiler error because the equals method in StringBuffer performs == operation, and == gives compiler error when objects are of different type?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is - the compiler gives error when it is evident the classes are of incomparable types. But in equals one of them get up-casted to Object reference and it doesn't give compiler error. I noticed that if you cast the compared objects to Object while using == operator it doesn't give error for incomparable types. So it might be only compile time check of the object reference type not the actual type of objects.
Someone please confirm this?!
[ August 28, 2007: Message edited by: B Misra ]
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StrngBuffer doesnt override equals() so based on objects method or ==
but because arguments are of different types it returns false,not Error

Check this link .http://www.witscale.com/scjp_studynotes/chapter9.html
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The '==' in Object#equals is performed with an Object reference type. Since every object in Java descends from Object the operation is perfectly legal.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer does not override the equal's method , so it works like as a == .

But euals method return type is boolean.
if both the Strings pointing to the same location it will return true , otherwise return false even both are different also it will return false
 
Something about .... going for a swim. With this tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic