Search code examples
javaobjecttypecasting-operator

Object Typecasting


In the following code:

A tat = new P();

P bat = (P) tat;

is typecasting tat: P bat = (P) tat;

the same as saying: P tat = new P();

Can you also say in theory that: P bat = ( P tat = new P(); )


Solution

  • The answer is yes but P bat = ( P tat = new P(); ) is not valid Java code and would not compile.

    You would have to define them using two statements:

    P bat, tat;
    bat = tat = new P();