• 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() ?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class test{
public static void main(String arg[]){
ice ob=new ice("5");
ice oc=new ice("5");
System.out.println(ob.equals(oc)); //1
Integer i=new Integer(5);
Integer j=new Integer(5);
System.out.println(i.equals(j)); //2
}
}
class ice{
ice(String s){
}
}
//1 : printout false
//2 : printout true
Why equals operator behaves differently. I know that the reference of ice object is different.Why it didn�t compare the content of ice object? And why it compare the content of Integer object?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asif,
For ice object the equals() method of the Object class is not overridden.
For Integer object the equal() method is overriden.
Hope this helps..
Jeban.
 
Muhammad Asif
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeban
Thanx for reply.One thing more how we distinguish that equals() overide or not.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need to go through the API for that one. If you create the class, you will need to override it yourself obviously, but for the other classes, go through the API and see if it is overridden. I believe all of the wrapper classes have an equal() method that should do what you want.
 
Muhammad Asif
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi bozeman,
Got it.Thanx.
Regards,
Asif
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any body can tell me that can we compare StringBuffer to String object through equals method
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)); //this one
System.out.println("Poddar".substring(3));
Thanx
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since <code>equals</code> method is defined on <code>Object</code> class, you can call it on any class ( since all classes derive from <code>Object</code> ). However if the class on which it is being called does not provide( "override" ) its own version of the method, then you will see the default behaviour which is reference-based comparison.
So, to answer your question, yes you can call the <code>equals</code> on the <code>StringBuffer</code> class passing in a <code>String</code> object. If you are wondering why it returns false, take a look at API documentation for the
<code>StringBuffer</code> class.
Ajith
 
He's giving us the slip! Quick! Grab this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic