Home  >  Article  >  Java  >  Concepts and differences between static binding and dynamic binding in Java Code analysis

Concepts and differences between static binding and dynamic binding in Java Code analysis

伊谢尔伦
伊谢尔伦Original
2017-07-17 15:59:301879browse

There are two binding methods in Java, one is static binding, also known as early binding. The other is dynamic binding, also known as late binding.

The concept of program binding:

Binding refers to the association of a method call with the class (method body) in which the method is located. For java, binding is divided into static binding and dynamic binding; or early binding and late binding

Static binding (early binding compiler binding):

The method has been bound before the program is executed, which is implemented by the compiler or other linker. For example: C. For java, it can be understood as binding during program compilation; in particular, the only methods in java are final, static, private and constructor methods are early binding

Dynamic binding (late binding runs Late binding):

Late binding: Binding based on the type of the specific object at runtime.

If a language implements late binding, it must also provide some mechanism to determine the type of the object during runtime and call the appropriate methods respectively. That is to say, the compiler still does not know the type of the object at this time, but the method calling mechanism can investigate by itself and find the correct method body. Different languages ​​implement late binding differently. Think of it this way: They all have to insert some special type of information into the object.

The process of dynamic binding:

  • The virtual machine extracts the method table of the actual type of the object

  • Virtual machine search method signature

  • Calling method

Summary about binding:

After understanding the concepts of the three, we found that java belongs to late binding. In Java, almost all methods are late bound. Dynamically bound methods belong to subclasses or base classes at runtime. But there are also special methods. Since static methods and final methods cannot be inherited, their values ​​can be determined at compile time. They are early binding. A special point to note is that methods and member variables declared privately cannot be inherited by subclasses. All private methods are implicitly designated as final (from this we know: declaring methods as final type is to prevent the method from being overwritten. , the second is to effectively turn off dynamic binding in java). Late binding in Java is implemented by the JVM. We do not need to declare it explicitly, but C++ is different. We must explicitly declare that a method has late binding. Upcasting or polymorphism in Java is achieved with the help of dynamic binding, so understanding dynamic binding means upcasting and polymorphism.

For methods in Java, except for final, static, private and constructor methods, which are pre-bound, all other methods are dynamically bound. Dynamic binding typically occurs under the conversion declaration of parent class and subclass:

For example: Parent p = new Children();

The specific process is as follows:

1. The compiler checks the declared type and method name of the object. Suppose we call the x.f(args) method, and x has been declared as an object of class C, then the compiler will enumerate all methods named f in class C and the f methods inherited from the super class of class C

2. Next, the compiler checks the parameter types provided in the method call. If among all the methods named f, there is a parameter type that best matches the parameter type provided by the call, then this method is called. This process is called "overload resolution"

3. When the program is running and uses dynamic When a binding calls a method, the virtual machine must call a version of the method that matches the actual type of the object pointed to by x. Assume that the actual type is D (a subclass of C). If class D defines f(String), then this method is called, otherwise the method f(String) is searched for in the superclass of D, and so on

Thinking about the problem:

How to provide method users with a method to complete a task. What if the user has special requirements and can he customize his own method?

Involved knowledge:

Child and parent classes, interfaces, upward transformation, dynamic binding

Specific code:


package com.chengxuyuanzhilu;
public interface MyInterfaces {
  void doting();
}
package com.chengxuyuanzhilu;
public class Drink implements MyInterfaces {
  @Override
  public void doting() {
    System.out.println("我在喝水");
  }
}
package com.chengxuyuanzhilu;
public class Eat implements MyInterfaces {
  @Override
  public void doting() {
    System.out.println("我在吃东西");
  }
}
package com.chengxuyuanzhilu;
public class Run implements MyInterfaces {
  @Override
  public void doting() {
    System.out.println("我在奔跑");
  }
}
package com.chengxuyuanzhilu;
public class TestDynamicBind {
  public static void main(String[] args) {
    MyInterfaces my = null;
    my = new Eat();
    bind(my);
    my = new Drink();
    bind(my);
    my = new Run();
    bind(my);      
  }
  static void bind(MyInterfaces my){
    my.doting();  
    }
}

Difference comparison

1. Static binding occurs at compile time, dynamic binding occurs at runtime
2. Use private or static or final Modified variables or methods use static binding. Virtual methods (methods that can be overridden by subclasses) will be dynamically bound according to the runtime object.
3. Static binding is completed using class information, while dynamic binding needs to be completed using object information.
4. The overloaded method is completed using static binding, while the overriding method is completed using dynamic binding.

Verification

Dynamic binding cannot be directly verified using javap, and if it is proved that static binding is not performed, then it means that dynamic binding is performed.

22:27 $ javap -c TestMain
Compiled from "TestMain.java"
public class TestMain {
  public TestMain();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return
 public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/String
       3: dup
       4: invokespecial #3                  // Method java/lang/String."<init>":()V
       7: astore_1
       8: new           #4                  // class TestMain$SubCaller
      11: dup
      12: invokespecial #5                  // Method TestMain$SubCaller."<init>":()V
      15: astore_2
      16: aload_2
      17: aload_1
      18: invokevirtual #6                  // Method TestMain$Caller.call:(Ljava/lang/String;)V
      21: return
}

The above is the detailed content of Concepts and differences between static binding and dynamic binding in Java Code analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn