简体   繁体   English

编译时间与运行时间

[英]compile time vs run time

After going through 2-3 hour to know, what is the difference between compile-time and run-time . 经过2-3个小时的了解后, compile-time和run-time有什么区别。 Lastly, i came up with this. 最后,我想到了这个。

Memory allocated at runtime referred to run-time/dynamic binding and allocated at compile time referred to compile-time/static binding. 在运行时分配的内存称为运行时/动态绑定,在编译时分配的内存称为编译时/静态绑定。

and then i tried this example 然后我尝试了这个例子

class myclass {

    void here() {
        System.out.println("Here from myclass !!");
    }

    void here(int i) {
        System.out.println("Here !!" + i);
    }
}

class thisclass extends myclass {

    void here() {
        System.out.println("Here from thisclass !!");
    }
}

public class poly {

    public static void main(String s[]) {
        myclass m= new myclass();
        myclass a= new thisclass();
        m.here();
        m.here(12);
        a.here();
        a.here(13);
    }
} 

So, i also found that myclass a= new thisclass(); 因此,我还发现myclass a= new thisclass(); is considered to be run-time binding. 被认为是运行时绑定。 Since, a is the object of myclass , but suddenly compiler found that, class mis-matched. 既然, amyclass的对象,但是突然编译器发现,class不匹配。 So, it will be dynamically bind the space of thisclass object. 因此,它将动态绑定thisclass对象的空间。

So, till here, i got the things. 所以,直到这里,我明白了。 But, i found that, another common answer was overloading refer to compile time and overriding refer to run-time . 但是,我发现,另一个常见的答案是重载是指编译时,而重载是指运行时 I didn't get this point. 我没有明白这一点。

thisclass a= new thisclass();
a.here();

Is this also called to be run-time binding. 这也称为运行时绑定。 ?? ?? Please correct me, if wrote anything wrong here. 如果在这里写错了什么,请纠正我。

First of all, memory allocation is not in this picture. 首先,该图中没有内存分配。 There is no compile-time memory allocation. 没有编译时内存分配。

The question conflates compile-time with static binding and run-time with dynamic binding. 这个问题使静态绑定的编译时间和动态绑定的运行时间混为一谈。

Static binding happens at compile time; 静态绑定发生在编译时; dynamic binding happens at runtime. 动态绑定运行时发生

Now, when you write 现在,当你写

myclass m= new thisclass();
m.here(18);

what happens at compile-time is the resolution of the method signature : you are calling here(int) and that choice is final. 编译时发生的是方法签名的解析:您正在调用here(int) ,而选择是最终的。 This is termed "static binding". 这被称为“静态绑定”。 What happens at runtime is method dispatch : the runtime chooses a here(int) implementation appropriate to the runtime type of the object referenced by m . 在运行时发生的是方法分派 :运行时选择适合于m引用的对象的运行时类型的here(int)实现。 There are two methods to choose from: myclass.m(int) and thisclass.m(int) , and the runtime chooses the latter in this particular example. 有两种方法可供选择: myclass.m(int)thisclass.m(int) ,运行时在此特定示例中选择后者。 This is termed "dynamic binding". 这被称为“动态绑定”。

As to your question "is overriding compulsory for dynamic binding"... The Java Language Specification prescribes the rules on choosing the correct method to invoke at runtime. 关于您的问题“对动态绑定是强制性的”……Java语言规范规定了选择正确的方法以在运行时调用的规则。 These rules imply a procedure known as "dynamic binding" for the general case. 这些规则意味着一般情况下称为“动态绑定”的过程。 But if you are asking whether any specific process always happens at runtime, the story is different: an optimizing JIT compiler can see that there is only one method to choose from and output a "monomorphic call site" which hardcodes the single choice. 但是,如果您问是否在运行时总是发生任何特定的过程,则情况就不同了:优化的JIT编译器可以看到只有一种方法可供选择,并输出一个“单态调用站点”,该站点对单项选择进行硬编码。 Further, it can also inline the entire method into the caller, thereby removing even the call itself. 此外,它还可以将整个方法内联到调用方中,从而甚至删除调用本身。

This: 这个:

thisclass a= new thisclass();
a.here();

is not run-time binding as the compiler knows what here() method to call (the one from thisclass). 不是运行时绑定,因为编译器知道要调用的here()方法(该类中的一个)。 But if you would say: 但是,如果您会说:

myclass a= new thisclass();
a.here();

then would be a run-time binding. 那么将是运行时绑定。

PS: your classes names should start with a capital letter. PS:您的班级名称应以大写字母开头。

But, i found that, another common answer was overloading refer to compile time and overriding refer to run-time. 但是,我发现,另一个常见的答案是重载指的是编译时,而重载指的是运行时。 I didn't get this point. 我没有明白这一点。

Overloading means having multiple methods with different parameters in the same class. 重载是指在同一类中具有多个带有不同参数的方法。 Which method is called is known at compile time, because the arguments are specified at this time. 在编译时就知道调用哪种方法,因为此时已指定了参数。

Overriding means re-defining a method from parent-class in the subclass. 重写意味着从子类中的父类重新定义方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM