Forums Register Login
Runtime and compile-time ?
Hi guys,

Do you exactly know what are the differences between the concepts compile-time and runtime? For example; some variable types are created at runtime, while others are created at compile-time. How do we understand this? By looking at the code, or knowing something? Could you define these two concepts please? Thanks.
Hi Ugur

To demonstrate the difference between runtime and compile time errors lets look at some simple code that causes these errors.

Compile time error

The above code will cause a compile time error because a semicolon is missing in the highlighted line.

Runtime time error
class RunTime
public static void main(String args[])
int a=5;
int b=0;
int c=a/b;
System.out.println("This will be a compile time error"+c);
The above code will give you a runtime error because in the line shown highlighted you are dividing a number by zero and assigning the same to an integer, thereby causing an ArithmaticException.
But notice that the above example will not cause a compile time error because the value assigned could have been something other than '0', say '2' which was shown in the CompileTime error program.

From the above two examples we can conclude that if the compiler can predict during compile time that an error will occur, such as an obvious error like a missing semicolon, it will result in a compile time error.
If the compiler cannot find any syntax error in your code, the program compiles fine.

Although the code causing a RunTime error may be syntatically correct, like the one in the RunTime example, but it might not be obeying the Java language specifications when the expression is actually executed.
In general, these two terms mean exactly what they say:

compile time - when a program is compiled
run time - when a program is run

Abdulla gave a good example of the difference between compile-time and run-time errors. To answer your question about class types, you need to understand inheritence. My favorite inheritence example is something like the following:

The difference between when the type of an object is determined is illustrated quite well here. At compile-time, the compiler only knows that the array "shapes" holds Shape objects. It doesn't know exactly which kind of shapes they are. However, at run-time, the JVM figures out which version of area() and perimeter() to call when appropriate.

I'm not sure what else I can say here, so I'll leave it open for any questions you may have. Understanding the code above will greatly help you learn about what information about an objects type is available at any given moment.


Whose rules are you playing by? This tiny ad doesn't respect those rules:
global solutions you can do at home or in your backyard

This thread has been viewed 1478 times.

All times above are in ranch (not your local) time.
The current ranch time is
Apr 23, 2019 07:57:45.