File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes equals and instanceof Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals and instanceof" Watch "equals and instanceof" New topic
Author

equals and instanceof

Syed Naimathullah
Greenhorn

Joined: Feb 18, 2007
Posts: 17
Hi friends, can you just explain what "equals" and "instanceof" used for.what is the difference between them.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Moved here from SCJP. This level of question belongs here.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
public boolean equals(Object o)
It is method defined in the Object class. equals() main concern to compare
whether two objects are meaningfully equal or not.

Person p1 = new Person("Mack");
Person p2 = new Person("Mack");

Here p1 and p2 can be considered meaningfully equivalent because both
objects encapsulate the same name "Mack". But for this to take place you
need to override the equals() method of the Object class in the Person class
otherwise you will get the default behavior of the equals() of the Object,
because every class inherits Object class methods.

Default behavior of equals(): Object class equals() does nothing
special but returns true if both references refer to the same object on
the heap. So it is same as you do p1==p2.

instanceof This is Java operator used to test whether a particular
object is an instance of given class.
You can use instanceof operator in this way:



cmbhatt
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
instanceof could be used to test not only class but for any reference type
including arrays and interfaces.


The Zen of Java Programming.
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
two things could be equals but not necessarily same.

equals is for testing if two objects are equal
whereas == is for testing if they are same.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
instanceof could be used to test not only class but for any reference type including arrays and interfaces


... and in particular to test any class that (directly or indirectly) extends Person.


Ping & DNS - updated with new look and Ping home screen widget
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
The test is on an instance at run time being of a particular type
or its subtype. (All classes implementing an interface are sub-types
of the interface type and all classes and interfaces and array types are
subtypes of Object).
If the reference is null then it would always return false.

Person p1 = null;
if (p1 instanceof Person) { // false

}

int[] a = new int[10];
if (a instanceof Object) { // true

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and instanceof
 
Similar Threads
equal() method useage
equals( ) method: use of instanceof
Buckets and Hashes
instanceof
isinstanceOf/ equals