Downcasting / Upcasting error at compile time and runtime?

Please check the program below.

I have doubts when the compiler will throw a casting exception at the compiler level and when will it be in runtime?

As in the program below, the expression

I assumed that there (Redwood) new Tree()should have been a failure during compilation since Tree is not Redwood. But he does not fail in compile time, as expected, that he did not pass in time runtime!!!

public class Redwood extends Tree {
     public static void main(String[] args) {
         new Redwood().go();
     }
     void go() {
         go2(new Tree(), new Redwood());
         go2((Redwood) new Tree(), new Redwood());
     }
     void go2(Tree t1, Redwood r1) {
         Redwood r2 = (Redwood)t1;
         Tree t2 = (Tree)r1;
     }
 }
 class Tree { }
+3
source share
6 answers

. . new Tree() Tree, (Redwood)new Tree() (Redwood)myTreeVariable.

+4

Redwood, , , .

, , , , , , . , , - , .

Tree t = new Redwood();
Redwood r = (Redwood) t;

, .

+5

.

       Tree
      /    \
     /      \
    /        \ 
Redwood       Blackwood  

:

: from the sub classes towards the root.

:

A Redwood Blackwood tree: So

Tree t1;
Tree t2;
Redwood r = new Redwood() ;
Blackwood  b = new Blackwood() ; 

t1 = r;    // Valid 
t2 = b;    // Valid   

Downcast: from the root class towards the children or subclasses. .

:

Redwood r = new Tree();  //compiler error, because Tree is not a Redwood 
Blackwood r = new Tree();  //compiler error, because Tree is not a Blackwood  

a Tree object , Redwood Blackwook, - .

:

Tree t1;
Tree t2;
Redwood r = new Redwood() ;
Blackwood  b = new Blackwood() ; 

t1 = r;    // Valid 
t2 = r;    // Valid     

Redwood r2 = (Redwood)t1; 
Blackwood  b2 = (Blackwood)t2  

[]

Redwood r = (Redwood) new Tree(); ?

Downcast:

source Redwood r = (Redwood) new Tree(); fist Tree Redwood.

:

Tree t = new Tree();`
Redwood r = (Redwood)t;  

, ,

[]

?

Redwood tree supper. .

t = ();

t Tree() Redwood(). .

, t, . - Redwood r = (Redwood)t;, t Tree class, .

[:]

instanceof:

casting/coercing , instanceof . *

:

instanceof . , , . :

if (t instanceof Redwood)
{
    Redwood r = (Redwood)t;
    // rest of your code
}

: - . : instanceof

+3

, ?

, " ". :

  • .

, (Redwood) new Tree() , , , JLS ( 5.5.1) .

, JLS :

" S () T (target), S T, - ."

" S " AND " T , | S | <: | T |, | T | <: | S |. ."

"| S | <: | T |" , S T T.

S Tree, T Redwood, Redwood Tree... .

, , "", . JLS , Java, . ( , ... .)


JLS , , :

:

Redwood r = (Redwood) new Tree();

Tree t = new Tree();
Redwood r = (Redwood) t;

Tree t1 = new Tree();  Tree t2 = new Redwood();
Redwood r = (Redwood) (someCondition ? t1 : t2);

Tree t = gardenStore.purchaseTree();
Redwood r = (Redwood) t;

, :

  • ? , .

  • ? ... .

  • ? , !

, , , , , Halting. , , , , , !

+1

, : Redwood , ,

Redwood r;
Tree t = r; // no casting needed

Java. , .

0
source

The specified tree may be Redwood in advance, so the code is compiled.

     Tree t1 =  new Redwood(); 
     Redwood r1 = (Redwood)t1;
     Tree t2 = (Tree)r1;

You can see that Tree () may already be Redwood.

0
source

Source: https://habr.com/ru/post/1529274/


All Articles