Type conversion between the class object and the Instanceof comparison operator

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Class type conversion:

1,Upward transition (small - and large), is the data type of the variable value assigned to the data types of variables.

For example:

Subclass to superclass, the String type to Object type two are upward transition.

Features: automatic transformation

2,Downward transition (large, small), is the data type of the variable to the value of small data types of variables.

For example: the parent class rotor, Object types to String types that are two downward transition.

Features: forcibly transferred

 1 package xiao.li;
 2 
 3 public class Type conversion type and Instanceof comparison operator {
 4     public void main(String[] args)
 5     {
 6         /*
 7          * Upward transition (small to large)
 8          */
 9         The dog dog = new The dog(); //Automatic transformation
10         /*
11          * Downward transition (in turn), must be forced to
12          */
13         The dog lDog =(German Shepherd) dog;
14     }
15 }
16 
17 class The dog
18 {
19     }
20 class The dog extends The dog
21 {
22     }
23 class German Shepherd Dog  extends The dog
24 {
25     }

The Instanceof object and class comparison operators

Syntax:     instanceof  class

The return type is boolean type, non true false

Determine whether an object belongs to a class of

Note: this comparison operators can not use, must be used in class and class exists when the use of inheritance

 1 package xiao.li;
 2 
 3 public class Type conversion type and Instanceof comparison operator {
 4     public void main(String[] args)
 5     {
 6         /*
 7          * Upward transition (small to large)
 8          */
 9         The dog dog = new The dog(); //Automatic transformation
10         /*
11          * Downward transition (in turn), must be forced to
12          */
13         if(dog instanceof The dog)//If the two place - no, if (dog instanceof dog) this is wrong
14         {
15             The dog lDog =(German Shepherd) dog;
16         }
17         
18     }
19 }
20 class The dog
21 {
22     }
23 class The dog extends The dog
24 {
25     }
26 class German Shepherd Dog  extends The dog
27 {
28     }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jessica at November 16, 2013 - 9:57 PM