- Java compile time and runtime
- Compile time and runtime in Java
- What is compile time in Java
- What happens at compile time in java ?
- What is compile time error in java ?
- Does java compiler generates .class file even if it throws compilation error ?
- What is Runtime in Java
- What is runtime error in java ?
- Difference between runtime and compile time
- Difference between compile time error and runtime error
- Difference between Compile-time and Run-time Polymorphism in Java
- Java
- Java
Java compile time and runtime
— Compile time
The compiler translates the source code into a code that can be read by a machine, such as a bytecode file that can be read by a jvm in java. Simply put, when compiling, the machine helps us check whether there are syntax errors in the code, wrong keywords, etc., to prepare for the subsequent class loading, so there will be no allocation of memory in this process. Operation.
This process refers to adding the compiled bytecode file (.class file) stored on the disk to the memory to run. During the running process, a series of type checks will be performed, such as space memory allocation, logical judgment some type of. Therefore, there will often be some unpredictable errors in this process.
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 > >
Constants modified by static and final at the same time are called compile-time constants, so number1 and number2 have been loaded at compile time, that is, the value of product1 has been determined during compilation. And number3 and number4 can only be successfully assigned when the memory space is allocated at runtime, so the value of product2 can only be determined at runtime. Decompilation is as follows:
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; > >
Method overload: This happens at compile time. Method overloading is also called compile-time polymorphism , Because the compiler can choose which method to use according to the type of the parameter.
public class Test public static void A(String param1); // method #1 public static void A(int param1); // method #2 >
If the method called by the compiler is as follows
Then it will find the method of menthod #1 by itself when compiling
Method coverage:This happens at runtime. Method overloading is called runtime polymorphism , Because the compiler does not know and cannot know which method to call at compile time. The JVM will make a decision while 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; > >
If the compiler encounters the following code, it will not be able to determine whether the passed parameter is of type A or type B at compile time. It can only be determined at runtime to determine whether to call method #3 or #4
public int evaluate(A reference, int arg2) int result = reference.compute(arg2); >
Generic (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 the generics into non-generic codes that can be executed on the current JVM. This technique is called «type erasure».
public class Test4 public static void main(String[] args) < ArrayList arrayList1=new ArrayList(); arrayList1.add("abc"); ArrayList arrayList2=new ArrayList(); arrayList2.add(123); System.out.println(arrayList1.getClass()==arrayList2.getClass()); > >
In this example, we have defined two ArrayList arrays, but one is of the ArrayList generic type and can only store strings. One is the ArrayList generic type, which can only store integers. Finally, we obtain the information of their classes through the getClass method of the arrayList1 and arrayList2 objects, and finally find that the result is true. Explain that the generic types String and Integer have been erased, only left Primitive type 。
Exception: Divided into compile-time exception and runtime exception
RuntimeException (RuntimeException) is also called unchecked exception (unchecked exception), which means that this kind of exception does not require a compiler to detect . RuntimeException is the parent class of all exceptions that can be thrown at runtime. In addition to catching exceptions, a method may throw if it is executed. A subclass of RuntimeException, then it does not need to use the throw statement to declare the thrown exception.
E.g: NullPointerException,ArrayIndexOutOfBoundsException ,and many more
Checked exceptions are checked by the compiler at compile time. They are also called compile-time exceptions. They are handled through throws statements or try<>cathch<> statement blocks. . The compiler will analyze which exceptions will be thrown when executing a method or constructor.
Compile time and runtime in Java
Runtime and compile time, these are two programming terms that are more frequently used in java programming language. The programmers specially beginners find it little difficult to understand what exactly they are. So let’s understand what these terms means in java with example.
In java running a program happens in two steps, compilation and then execution. The image below shows where does compile time and runtime takes place in execution of a program.
What is compile time in Java
After writing the program, programmer needs to compile that program. As soon as the programmer starts compiling the program using javac compiler, the compile time gets started, and it ends when either a .class file is generated after successful compilation or an error is thrown in compilation. In other way we can say, the process of compiling a program is referred as compile time.
For an example if you wrote a program and saved it as MyFirstProgram.java , Once you start compiling this program using javac command as javac MyFirstProgram.java , the compile time gets started and it ends when a .class file as MyFirstProgram.class is generated or any error is thrown in compilation.
What happens at compile time in java ?
At compile time, the java compiler(javac) takes the source code(.java file) and checks if there is any syntax, type-checking or any semantic errors inside the program. If there is no error, the compiler generates a .class(bytecode) file for that .java file. If there is any compilation error, java compiler displays that error in command window(eg cmd).
What is compile time error in java ?
If a program element(class, method, variable, statements etc) is not written as per it’s syntax in java, the compiler throws error for that element while compiling the program. We call these errors as compile time errors as these errors are detected at compile time by the java compiler.
Does java compiler generates .class file even if it throws compilation error ?
No, it will not generate .class file, it will only display the compilation error in console(eg. cmd) window.
Let us see compile time error by an example. The most common mistake that beginners do is, they forget to add semicolon(;) at the end of a statement which results as a compilation error while compiling the program.
class
MyFirstProgram <public static void
main(String [] args) < System.out.println("first statement"
)// missing semicolon(;)
System.out.println("second statement"
); > >
Once you compile above program as javac MyFirstProgram.java , it will display a compilation error in console window like below :
MyFirstProgram.java:3: error: ';' expected System.out.println("first statement") // missing semicolon(;) ^ 1 error
What is Runtime in Java
As soon as the programmer starts executing the program using java command, runtime gets started and it ends when execution of program ended either successfully or unsuccessfully. In other way the process of running a program is known as runtime.
For an example if you wrote a program and saved it as MyFirstProgram.java . After compilation when you execute the command java MyFirstProgram for running the program, runtime gets started and it ends when either the output of program is generated or any runtime error is thrown.
What is runtime error in java ?
Errors which comes during the execution(runtime) of a program are known as runtime errors. If a program contains runtime error, it won’t run successfully, rather that error will be displayed in command window(eg. cmd) at the time of execution.
Let us suppose if you wrote a program MyFirstProgram.java like below, After compilation when you run the below program using java MyFirstProgram command, it will throw a runtime error.
class
MyFirstProgram <public static void
main(String [] args) <int
num1 = 10;int
num2 = 0; System.out.println(num1/num2);// Runtime error: Divide by zero exception
> >
Exception in thread "main" java.lang.ArithmeticException: / by zero at MyFirstProgram.main(MyFirstProgram.java:5)
Difference between runtime and compile time
Compile time is a process in which java compiler compiles the java program and generates a .class file. In other way, in compile time java source code(.java file) is converted in to .class file using java compiler. While in runtime, the java virtual machine loads the .class file in memory and executes that class to generate the output of program.
Difference between compile time error and runtime error
Compile time errors are the error that comes while compiling the program whereas runtime errors are errors that comes at the time of execution(run-time) of the program. An example of compile time error is «not adding a semicolon(;) at the end of a statement» in your java program while «dividing a number by zero» is an example of runtime error.
- Without compilation you can not run the program, compile first then run.
- The output of a program is generated in runtime, not in compile time.
- If you made any changes in program after compilation, you need to compile the program again to see the changes in output.
Difference between Compile-time and Run-time Polymorphism in Java
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. In this article, we will see the difference between two types of polymorphisms, compile time and run time.
Compile Time Polymorphism: Whenever an object is bound with its functionality at the compile time, this is known as the compile-time polymorphism. At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism or static or early binding. Compile-time polymorphism is achieved through method overloading. Method Overloading says you can have more than one function with the same name in one class having a different prototype. Function overloading is one of the ways to achieve polymorphism but it depends on technology and which type of polymorphism we adopt. In java, we achieve function overloading at compile-Time. The following is an example where compile-time polymorphism can be observed.
Java
Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. The runtime polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile time. It is also called dynamic or late binding. Method overriding says the child class has the same method as declared in the parent class. It means if the child class provides the specific implementation of the method that has been provided by one of its parent classes then it is known as method overriding. The following is an example where runtime polymorphism can be observed.
Java
The following table demonstrates the difference between runtime polymorphism and compile-time polymorphism:
Compile Time Polymorphism | Run time Polymorphism |
---|---|
In Compile time Polymorphism, the call is resolved by the compiler. | In Run time Polymorphism, the call is not resolved by the compiler. |
It is also known as Static binding, Early binding and overloading as well. | It is also known as Dynamic binding, Late binding and overriding as well. |
Method overloading is the compile-time polymorphism where more than one methods share the same name with different parameters or signature and different return type. | Method overriding is the runtime polymorphism having the same method with same parameters or signature but associated withcompared, different classes. |
It is achieved by function overloading and operator overloading. | It is achieved by virtual functions and pointers. |
It provides fast execution because the method that needs to be executed is known early at the compile time. | It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime. |
Compile time polymorphism is less flexible as all things execute at compile time. | Run time polymorphism is more flexible as all things execute at run time. |
Inheritance is not involved. | Inheritance is involved. |