What is the difference between binding and dispatching in Java?

Too many related names: early and late binding, static and dynamic dispatch, runtime and polymorphism of compilation time, etc., which I don’t understand the difference.

I found a clear explanation , but is this true? I rephrase JustinC:

Binding: defines the type of variable (object?). If this was done at compile time, its early binding. If this was done at run time, this completes the binding.

Submit: Determines which method corresponds to the method call. Static Dispatch are computational methods at compile time, while dynamic dispatch does this at runtime.

Is the binding consistent with primitive and reference variables with primitive values ​​and objects, respectively?

Edit: Please give me some clear reference materials so that I can learn more about this.

+4
source share
3 answers

I find confusion usually arises from how overloaded these terms are.

We program our programs in a high-level language, and either the compiler or the interpreter must transform this into something that the machine really understands.

, - . , , , , , ?.

, - , . , , , , .

static dynamic , , .

, . , .

- . . , .

, , , .., .

I , , Java.

. , .

+4

, : - ( ) /, , , , , :

class A {
    void methodX() {
    System.out.print("i am A");
    }
 }

A methodX(), , ,

class B extends A {
  void methodX() {
    System.out.print("i am B");
   }
 }
....
A objX= new B();
objX.methodX();

x , / ( , ).

0

" ", , - JLS. , , , .

.

15.12.2. 2:

, ,   .    , ,    ,   .

     

,    . (   ) - , ,    . ,   

, " " , 15.12.2.5. .

" ",

JLS 12.5. :

++ Java    .    ,   , ,    .

12.5-2.

class Super {

  Super() {
      printThree();
  }

  void printThree() {
      System.out.println("three");
  }
}

class Test extends Super {

  int three = 3;

  void printThree() {
      System.out.println(three);
  }

  public static void main(String[] args) {
      Test t = new Test();
      t.printThree();
  }
}

:

0
3

, Super printThree, - Test, , .

.

15.11.1-1. . :

class S {
    int x = 0;
    int z() { return x; }
}

class T extends S {
    int x = 1;
    int z() { return x; }
}

public class Test1 {

    public static void main(String[] args) {
        S s = new T();
        System.out.println("s.x=" + s.x);
        System.out.println("s.x=" + s.z());
    }
}

:

s.x = 0
s.x = 1

, " ", " ":

. , .

8.

(Β§8.1.2), .. , .

, 2 List<String>, String .

:

4.8.

class Outer<T>{
    T t;
    class Inner {
        T setOuterT(T t1) { t = t1; return t; }
    }
}
     

() Inner Outer. Outer , Inner , T .

, Outer outer ( ) T ( - ).

0

Source: https://habr.com/ru/post/1666097/


All Articles