Operators is Java
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, May 6, 2003 01:12 PM

My question is regarding the instanceof operator in java.

i have read a few books on java to find out about instanceof operator including Java: How to Program by Deitel & Deitel & The Complete Java 2 Certification Study Guide by Simon Roberts, Phillip Hellp & Michael Ernest. but i am stil unable to understand the working of instanceof operator. please explain it to me with a code.

secondly, please tell me if it is still included in the certification objective of SCJP JDK 1.4.1.

Re: Operators is Java

Posted By:   Anonymous  
Posted On:   Friday, June 6, 2003 03:01 AM

The instanceof operator is still on the Java 1.4 programmer certification program.

It is actually important to understand what it does and... why you should avoid it as much as possible.

In short: this operator lets you check the type of the object your are dealing with. Basically you ask the system: "is this object of type MyType?" (replace MyType by the class you want to check).



It is a very important and "dangerous" operator in the language. Basically, it is often shown in code inside a nested if statement. It is even presented like this in the SUN course at this date (I am delivering this training myself). But you have to be aware that using it for branching statements toward objects is breaking the idea of polymorphism. So, the situations where you have to use instanceof obviously exist but as a general rule, every time you are using it, you should spend some time on your design, wondering why you are using it and how you could do another way!
But beware, I am not saying you should never use it. Sometimes, you have to. But I would recommend it mainly for advanced users. A beginner using that is usually programming without respecting the basis of OO.




S.L.

www.liemur.com

Re: Operators is Java

Posted By:   Simon_Ablett  
Posted On:   Wednesday, May 14, 2003 10:24 AM

What exactly is it that you don't understand?

Regards.

Re: Operators is Java

Posted By:   Sandeep_Shilawat  
Posted On:   Wednesday, May 14, 2003 10:15 AM

Object obj = new Date();

if(obj instanceof Date){
// Do something !!!
}
About | Sitemap | Contact