wood burning stoves*
The moose likes Java in General and the fly likes Runtime and compile-time ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime and compile-time ?" Watch "Runtime and compile-time ?" New topic
Author

Runtime and compile-time ?

Ugur Likoglu
Greenhorn

Joined: Jul 20, 2004
Posts: 14
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.


--ugur<br />"Read,think, and ask. Learn the reason of living."
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
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.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.

HTH

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime and compile-time ?