Java static type checking sample code explanation

Explanation of static type checking and dynamic type checking:

Static type checking: the process of verifying type safety based on the source code of the program;

Dynamic type checking: the process of verifying type safety during program operation;

Java uses static type checking to analyze programs during compilation to ensure that there are no type errors. The basic idea is not to let type errors occur during runtime.

In all kinds of programming languages, there are two type checking mechanisms: static type checking and dynamic type checking.

Static type checking is to ensure the type safety of the program during compilation by analyzing the source code of the application.

Dynamic type checking is to verify the type safety of the program during the operation of the program. In Java, the static type checking mechanism is used to analyze the Java source code during compilation, and the errors missing types in Java can be found in advance. The advantage of this mechanism is that the wrong things are not found at run time. In this article, we will use several code examples to explain the type checking mechanism in Java in depth. Once you fully understand the examples in this article, you have a complete grasp of static type checking in Java.

Deep analysis of static type checking in Java

Code example

Suppose we have the following two classes, class Super and class suber, and they have the following inheritance relationship:

First, think about this question: "new suber() What is the return value of "me()"? Is it a super object or a suber object?

The me () method is declared to return a super object, so it is considered to return a super object during compilation. However, during runtime, the statement actually returns a suber object, because suber inherits super's me () method and returns its own object (polymorphism).

Static inspection

Let's take a look at the common error example code:

This statement will report an error during compilation. Although the result returned by "new suber(). Me()" is essentially a suber object, the suber object does also have a dosuber () method. However, in the compiler's view, "new suber() The reference type of the result returned by "me()" is super. Naturally, you cannot call the dosuber() method that is not defined in the super type reference. Therefore, we can change it to the correct form as follows:

In addition, since the result returned by "new suber(). Me()" is indeed a suber object, we can use forced type conversion to complete the function if we want to call its unique method:

Deep expansion

Now, suppose we add a class as follows: brother

So, let's think about the following sentence:

Is this statement really correct? In fact, the statement does pass static type checking and compile correctly. After all, the compiler does not know the actual type of "new suber(). Me()". However, the statement throws the following error at run time:

The reason is very simple. Two classes that do not have any inheritance relationship (suber and brother) cannot be type converted.

summary

The above is a detailed explanation of the Java static type check example code introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>