Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Runtime and compile-time ?  RSS feed

Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!