java compiles and details runtime concepts and instances

  • 2020-07-21 07:50:52
  • OfStack

Java compile and run time are important concepts, but 1 is not clear, this special blog explains the concepts.

Basic concept

Compile time

Compile time is when you are compiling. The compiler translates your source code into machine-readable code.(In a general sense, of course, it might actually be a language that translates into an intermediate state. Java is a bytecode that is only recognized by JVM.

The compile time is simple to make 1 some translation work, such as checking man careless did you write the wrong what keywords. Have what lexical analysis, syntax analysis process. As a teacher in the composition of the students have the wrong character and pragmatically 1 sample. If found that the compiler will tell you what mistake. So sometimes 1 some people say that compile time also allocates memory what is definitely wrong.

The runtime

A runtime is code to run up. To be loaded into memory. (your code is stored in the disk is a dead guy before loaded into memory. Become a live only to run into memory). And run-time type checks and speak in front of the compile-time type checking (or static type checking) no. 1 is not a simple scan code. But in memory to do the operation, to do judgment. (can't find so many compile time errors in operation can find an error, it is better to write when they avoid that logic errors)

For example


int arr[] = {1,2,3}; 
int result = arr[4]; 
System.out.println(result); 
Exception in thread  " main "  java.lang.ArrayIndexOutOfBoundsException: 4 

Look at the code above, you know it's the wrong code, the array is out of bounds, but the compiler didn't report an error,run came after ArrayIndexOutOfBoundsException. So the compiler is actually pretty dumb, not as smart as your brain, so you think it's a little dumb, but it's not so bad when it runs.

The interview questions


 Understanding these concepts will help you better understand them 1 Some basic principles. Here's what beginners need to know to advance to intermediate levels 1 Some problems.  
Q. In the following code snippet, line A And line B What is the difference between the code identified? 

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 line A's code, the value of product is computed at compile time and line B is computed at run time. If you decompile ConstantFolding.class files using the Java decompiler (for example, ES36en-ES37en), you will get the 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. Since the values of the final variables do not change, they can be optimized. Both the Java decompiled and javap commands are powerful tools for looking at compiled code (for example, bytecode).

Method overloading: This happens 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 argument.


public class {
 public static void evaluate(String param1); // method #1
 public static void evaluate(int param1); // method #2
}

If the compiler compiles the following statements:

1evaluate(“My Test Argument passed to param1”);

It generates the bytecode that calls the #1 method, based on the fact that the parameter passed in is a string constant

Method override: This happens at run time. Method overloading is called runtime polymorphism because at compile time the compiler does not know and has no way of knowing which method to call. JVM makes decisions as the code runs.


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 subclass B (..) Method overrides the parent class's compute(..) Methods. If the compiler encounters the following code:


public int evaluate(A reference, int arg2) {
 int result = reference.compute(arg2);
}

The compiler has no way of knowing whether the parameter reference is of type A or B. Therefore, method #3 or method #4 can only be called at run time, depending on the type of object assigned to the input variable "reference" (for example, A or an instance of B)

Generics (also known as type checking) : This happens at compile time. The compiler is responsible for checking the correctness of the types in the program and then translating or rewriting the code that USES generics into non-generic code that can be executed on the current JVM. This technique is called type erasure.

In other words, the compiler erases all type information in Angle brackets to ensure compatibility with version 1.4.0 or earlier versions of JRE.

1List myList = new ArrayList(10);

This compiles to:

1List myList = new ArrayList(10);

Exceptions (Exception) : You can use run-time or compile-time exceptions.

Runtime exceptions (RuntimeException) are also known as undetected exceptions (unchecked exception), which means that such exceptions do not need to be detected by the compiler.

RuntimeException is the parent of all exceptions that can be thrown at runtime. In addition to catching exceptions, 1 method may be thrown if it executes

A subclass of RuntimeException, so it does not need to use the throw statement to declare the exception thrown.

For example: NullPointerException, ArrayIndexOutOfBoundsException, etc

Checked exceptions (checked exception) are checked at compile time by the compiler and handled by the throws statement or try{}cathch{} block. The compiler analyzes which exceptions are thrown when executing a method or constructor.

I hope you found this article helpful


Related articles: