Compile-time, run-time, build-time (i)

Source: Internet
Author: User

At the time of development and design, we need to consider the three concepts of compile-time, runtime, and build-time. Understanding these concepts can be a better way to help you understand some basic principles. Here are some questions that beginners need to know to qualify for intermediate level.

Q. What is the difference between the code identified by line A and line B in the following code fragment?

public class Constantfolding {

static final int number1 = 5;

static final int number2 = 6;

static int number3 = 5;

static int number4= 6;

public static void Main (string[] args) {

int product1 = Number1 * NUMBER2; Line A

int product2 = Number3 * NUMBER4; Line B

}

}

A. In the code for line A, the value of product is calculated at compile time, and Row B is calculated at run time. If you use the Java anti-compiler (for example, Jd-gui) to decompile the Constantfolding.class file, you will get an answer from the following results.

public class Constantfolding {

static final int number1 = 5;

static final int number2 = 6;

static int number3 = 5;

static int number4 = 6;

public static void Main (string[] args) {

int product1 = 30;

int product2 = Number3 * NUMBER4;

}

}

Constant folding is an optimization technique used by the Java compiler. Because the values of the final variables do not change, they can be optimized. Both the Java anti-compiler and the JAVAP command are a powerful tool for viewing compiled code (for example, bytecode).

Q. Can you think of any situation where it would be helpful to look at compiled code in addition to code optimization?

Generics in A.java are constructed at compile time and can be understood by looking at the compiled class file, or by looking at it to solve generic-related problems.

Q. Which of the following occurs at compile time, at run time, or both?

A. Method overloading: This occurs at compile time. Method overloading is also known as compile-time polymorphism, because the compiler can choose which method to use based on the type of the parameter.

public class {

public static void Evaluate (String param1); Method #1

public static void Evaluate (int param1); Method #2

}

If the compiler wants to compile the following statement:

Evaluate ("My Test Argument passed to param1");

It generates a byte code that calls the # # method, based on the parameters passed in as String constants

Method overrides: This occurs at run time. The method overload is called run-time polymorphism because the compiler does not know at compile time and cannot know which method to invoke. The JVM will make a decision when the code is running.

public class A {

public int compute (int input) {//method #3

return 3 * input;

}

}

public class B extends A {

@Override

public int compute (int input) {//method #4

return 4 * input;

}

}

Compute (..) in sub-category B The Compute method overrides the parent class's (..) Method. If the compiler encounters the following code:

public int Evaluate (A reference, int arg2) {

int result = Reference.compute (ARG2);

}

The compiler is not able to know whether the type of the passed parameter reference is a or B. Therefore, it is only possible at run time to determine whether to invoke method # or method # #, depending on the type of object assigned to the input variable "reference" (for example, an instance of a or B).

Compile-time, run-time, build-time (i)

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.