Total members 11121 |It is currently Fri May 24, 2013 12:10 am Login / Join Codemiles

All times are UTC [ DST ]



  java instanceof keyword
Fri Jan 30, 2009 1:06 am 

Java instanceof keyword :
This is an example on java instanceof keyword .instanceof keyword is used when avoid invalid casting in the runtime .

Code:
public class Main {

    public static void main(String[] args) {
           
         A classA=new A();
         B classB=new B();
         
         if(classA instanceof A)
         {
             System.out.println("Correct");
         }
         else
             System.out.println("Wrong ");
         if(classA instanceof Base)
         {
             System.out.println("Correct");
         }
         else
             System.out.println("Wrong ");
         
         if(classB instanceof A)
         {
             System.out.println("Correct");
         }
         else
             System.out.println("Wrong ");
         
         if(classB instanceof Base)
         {
             System.out.println("Correct");
         }
         else
             System.out.println("Wrong ");
         
         
    }

}
interface Base
{
   
}
class A implements Base
{
     public A()
     {
         
     }
}
class B extends A
{
    public B()   
    {
       
    }
}

If you have a part of your system generating a multiple types of objects , you will need to know the types of these objects before you start your logic on it .

_________________
Please recommend / share my post if you found it helpful.


Author:
Mastermind
User avatar Posts: 2788
Have thanks: 71 time
TOP
   
 
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : java instanceof keyword
 Java volatile keyword     -  
 Is null a keyword     -  
 switch keyword in c++ usage     -  
 Java Persistence Experts Teaches JPA, EJB, Java EE at GIDS 2     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 need help in java     -  
 Using FTP in java     -  
 Java course     -  
 java or .net     -  
 what is java     -  

All times are UTC [ DST ]



Jump to:  
cron



Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007->Today
mileX v1.0 designed by codemiles team