The following is from Wikipedia, explaining 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 execution;
Java uses static type checking to parse programs during compilation to ensure that there are no type errors. The basic idea is not to let type errors occur during the run.
The following code is an example that understands him and you will better understand how Java static type checking works.
code example
Suppose we have the following class, a and b,b inherit a.
Class A {
a Me () {return this
;
}
public void DoA () {
System.out.println (' do A ');
}
Class B extends A {public
void DoB () {
System.out.println (' do B ');
}
First, call new B (). What does Me () return? A object or B?
The Me () method is declared to return a object, so during compilation, the compiler only knows that it returns a object. However, it returned a B object while it was running, because B inherited a method that returned itself.
How does a static type check work?
The following line of code is illegal, even if the method dob () is invoked by a B object. The problem is that its reference type is a, in the compiler, the compiler does not know its true type, so use it as a type.
So, only the following code can be invoked:
However, we can convert the type of coercion to B, which is the following code:
Next, we add a C class:
Class C extends a{public
void Dobad () {
System.out.println (' do C ');
}
The following code statement is then checked by a static type:
The compiler does not know its true type, but will throw an exception during run time because the B type cannot be converted to type C;
The above is a small set of Java in the static type of inspection is how to carry out a practical example of the relevant knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!