This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Checking for object type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Checking for object type" Watch "Checking for object type" New topic
Author

Checking for object type

T sandya
Ranch Hand

Joined: Aug 03, 2006
Posts: 64
Hi Friends,

public class A{

public void findObject(){
Apple a = new Apple();
Apple b = new Apple();

boolean flag = compare(a,b);
}

public boolean compare(java.lang.Object o1,java.lang.Object o2){
-----
-----
}

}

here my question is from findObject() we are calling compare() method with two apple objects.

In compare() method we are declaring the parameters as java.lang.Object 's.

In compare() method how to fine these two objects are Apple objects?

Thanks in adavance.

Sandaya.
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
This is probably a larger question that you might realize.

The Java language provides an operator "instanceof" to check the runtime type of an object.



It has been said that, in most cases, instanceof should not be used to test for object equality as it can break the symmetric property of the equals method's equivalence relation. In this case, using the getClass method of Object is the appropriate choice. This makes a lot of sense.

The getClass example would work in a similar manner:



As an even further alternative, you may consider using something like this:



To determine if o1 is a superType pf o2. This may or may not be useful, depending on how you wish to compare o1 and o2
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

if (a instanceof Apple) { ...


[Jess in Action][AskingGoodQuestions]
T sandya
Ranch Hand

Joined: Aug 03, 2006
Posts: 64
hi friends,
here one more thing i need to mention is,
now i can tell the this Apple object, so we can apply InstanceOf on Apple object.

if i dont know it is the Apple object, with which object i need to check the object with instanceOf operator?

thanks in adavance.
srinu reddy
Greenhorn

Joined: Oct 21, 2007
Posts: 12
follow this approach

public boolean compare(java.lang.Object o1,java.lang.Object o2){
if (o1.getClass().equals(o2.getClass())) {
//Perform comparisson and return true if so
} else {
return false;
}
}
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by honey:
follow this approach


I don't think one can be prescriptive like this. There are two possible approaches (treat different subclasses as possibly equal, or treat them as definitely unequal). Each approach could be appropriate in some circumstances and inappropriate in others.

It's one of those cases where you have to think carefully, make your choice, and explain it in the javadoc comments.

If your equals() method is not expecting to deal with different subclasses, you could add an assertion to check that it doesn't get given them. Or, if there is no need for subclasses in your application, you could declare the class "final", and use the javadoc to explain that this is to avoid potential confusion when comparing objects.
[ October 29, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Checking for object type