Examples of how static type checking is done in Java _java

Source: Internet
Author: User

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.

Illegal

So, only the following code can be invoked:

Legal

However, we can convert the type of coercion to B, which is the following code:

Legal

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:

Legal

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.