aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "equals() ?" Watch "equals() ?" New topic
Author

equals() ?

Muhammad Asif
Greenhorn

Joined: Oct 17, 2000
Posts: 6
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?
Jonathan Jeban
Ranch Hand

Joined: Oct 08, 2000
Posts: 52
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

Joined: Oct 17, 2000
Posts: 6
Hi Jeban
Thanx for reply.One thing more how we distinguish that equals() overide or not.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Oct 17, 2000
Posts: 6
Hi bozeman,
Got it.Thanx.
Regards,
Asif
Ahmad Mudassir
Greenhorn

Joined: Oct 16, 2000
Posts: 10
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
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5781
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

Open Group Certified Master IT Architect.
Sun Certified Architect(SCEA).
 
 
subject: equals() ?
 
Threads others viewed
K&B page398
Usage of indexOf method of ArrayList class
Integer
Integer.equals(Long)???
regarding scjp 1.5
developer file tools