aspose file tools*
The moose likes Beginning Java and the fly likes Compilation error or Runtime exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation error or Runtime exception?" Watch "Compilation error or Runtime exception?" New topic
Author

Compilation error or Runtime exception?

john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
class Eco {
public static void main(String[] args){
Eco e1 = new Eco();
Eco e2 = new Eco();
Eco e3 = new Eco();
e3.e = e2;
e1.e = e3;
e2 = null;
e3 = null;
e2.e = e1;
e1 = null;
}
Eco e;
}


In the above program, how 'would' I tell whether it will be a compilation error or Runtime exception ( caused by e2.e = e1 ), at compile time.
Thanks.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

how 'would' I tell whether it will be a compilation error or Runtime exception ( caused by e2.e = e1 ), at compile time.


Firstly the compile time error is occurred , when something goes wrong according to the rules or code not adhered to syntax or semantics !(cant explain you more on this, please googled it) .

And runtime exception is because , while running the things are not same as we expected, then JVM complains !

Lastly, you ll required a lots of code writing practice, which makes you better programmer in future and you ll find this difference easier ,


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
john smith
Greenhorn

Joined: Jun 04, 2008
Posts: 14
Thank you for your response.

However, this code is a question in a certification book. The answer for the question suggests it is a runtime exception instead of a compilation error. I was thinking that the compiler would see a null pointer in the code and cause a compile error. Why is it a runtime exception instead of a compile time error?

Thanks.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Because , when compiler compiles the code, it saw everything is perfect, as per "rules" and and this statement e2.e = e1; has a valid reference "e2" , which pointing to nothing "null" object and retrieving its 'e' instance variable (object here) . This is fine as per compilers perspective.

When code actually runs, we are retrieving the 'e' instance variable , from an "null" nothing object, hence the JVM , complains that "he cant find the things as he expected" and he throws an runtime exception , stating "NullPointerException" ..

Hope this help !
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39571
    
  27
Pointers being null is something that occurs at runtime; the compiler has no concept of what a particular piece of code might or might not do at runtime, so it doesn't try to guess what might go wrong. It can't do that in general anyway, so why bother doing it for simple cases where that's possible?


Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation error or Runtime exception?
 
Similar Threads
Garbage Collection question
SCJP i need help in garbage collection
SCJP......Garbage collection
How can we determine whether an object's reference variable is null or not?
Islands of isolation