Java terminology for differentiating runtime type from compile-time type

前端 未结 8 1654
孤街浪徒
孤街浪徒 2020-12-06 08:56

In Java, an Object can have a runtime type (which is what it was created as) and a casted type (the type you have casted it to be).

I\'m wondering what

相关标签:
8条回答
  • 2020-12-06 09:39

    I think it's important to distinguish between the object (which exists at execution time, and just has its execution time type) and an expression (such as a variable) which has a compile-time type.

    So in this case:

    A a = new B();
    

    a is a variable, of type A. Its value at execution time is a reference to an object of type B.

    The Java language specification uses "run-time class" (e.g. for the purpose of overriding, as in section 15.12.4.4) for the type of an object. Elsewhere I think it just uses "type" for the type of an expression, meaning the compile-time type.

    0 讨论(0)
  • 2020-12-06 09:39

    I would say that you differentiate between the type of the variable/reference and the type of the object. In the case

    A a = new B();
    

    the variable/reference would be of type A but the object of type B.

    0 讨论(0)
提交回复
热议问题