What is the reason behind dynamic method parsing in statically typed languages like Java

I'm a little confused about Java's concepts of dynamic / static types and dynamic method parsing

consider:

public class Types {

    @Override
    public boolean equals(Object obj){
        System.out.println("in class Types equals()");
        return false;//Shut-up compiler!
    }

    public static void main(String[] args){
        Object typ = new Types();
        typ.equals("Hi");//can do this as String is a subclass of Object
    }
}

First: the reference variable typ is a type, isn't it?!

So, why are statically typed objects and dynamically typed objects packaged for method coverage?

Second: does the compiler have enough information to call the correct equals()?

If the class type does not have overridden equals (), it can call object Equals() method

In this case, the class has and the compiler knows it

Why can't this bind early like overloading? Why leave it to the JVM?

Solution

The reference variable typ in the example is of type object

The object it references is of type

The compiler does not have enough information to know that the object is actually types and uses type equals(Object).

This can be confusing If you write

Object typ = new Types();

Then make sure the compiler knows what types are in typ It just compiles this information into code!

But what if you decide to change the row to?

Object type = new ObjectFactory().choose(choice).use(decision).build();

Well, now you don't actually know what to build After objectfactory decides how to handle the values of selection and decision, this will only be known at run time

So in this case, the compiler doesn't know The only information available is the static type of the variable If the compiler behaves differently when using new than when using the above factory, it will be very bad

Now look at this scenario:

public class Types {
     public boolean equals( Object obj ) {
        // something
     }

     public boolean equals( String str ) {
        return false;
     }
}

public class Main {

    public static void main(String[] args) {
       Object typ = new Types();
       System.out.println( typ.equals("foo" ) );
    }
}

Now, in this case, types and main are two independent compilation units (different files, etc.) Suppose the compiler decides based on the fact that typ is types Then it will use types equals(String).

But now you can edit the type, delete equals (string), and recompile only the type When you run the program, you will get a "no such method exception" because the compiler assumes that types has an equals (string) method So your program failed, even though it is a fully legal Java program, even though there is a correct matching method equal (object) in the new type

Therefore, a real compiler uses static types to determine whether a method exists and is legal for a given static parameter, and only at runtime, the JVM will be able to find the content of the actual type and call the applicable override of the method

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
分享
二维码
< <上一篇
下一篇>>