Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Type Comparison Operator 
 

Java provides a run-time operator instanceof to compare a class and an instance of that class. This operator " instanceof" compares an object to a specified class type.

 

Type Comparison Operator

                         

Java provides a run-time operator instanceof to compare a class and an instance of that class. This operator " instanceof" compares an object to a specified class type.
The instanceof operator is defined to know about an object's relationship with a class. It evaluates to true, if the object or array is an instance of the specified type; otherwise it returns false. The instanceof operator can be used with the arrays and objects. It can't be used with primitive data types and values. Its signature is written as:

object  instanceof  type

Lets have an example :

class X { 
int i, j; 

class Y { 
int i, j; 

class extends X { 
int k; 


public class InstanceOfDemo { 
public static void main(String args[]) { 

X x = new X(); 
Y y = new Y(); 
Z z = new Z();

if(x instanceof X) 
System.out.println("x is an instance of X"); 

X obj; 
obj = z; // X reference to z

if(obj instanceof Z) 
System.out.println("obj is an instance of Z"); 
}
}

.Output of the Program: 

C:\nisha>javac InstanceOfDemo.java

C:\nisha>java InstanceOfDemo
x is an instance of X
obj is an instance of Z

In this example the class "Z" extends the class "X". So the expression " if (x instanceof X)" returns true because the object of class "X" has the reference to the object of class "Z". On the other hand, this example will generate an error, if the expression is written as "if (y instanceof X)" because the object "y" of class "Y" is not an object of class "X".

Download this Program

                         

» View all related tutorials
Related Tags: c class io vi state ie to behavior avi pic top ear e des li art in part as sta

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.