File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Runtime error vs compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime error vs compiler error" Watch "Runtime error vs compiler error" New topic
Author

Runtime error vs compiler error

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
If you a given some code, and asked what kind of error it might generate, is there any way to figure it out?
What are the benefits of encapsulation?
If the String class overrides equals(), then should any other method also be overriden to prevent an error?


May the force of the Java be in all of us !!!
Alain Boucher
Ranch Hand

Joined: Feb 25, 2003
Posts: 51
Compilation error Vs Runtime Error
Compilation Error = You cannot compile...
Runtime Error = Exception during runtime...
You try to call a method on a Object but the Object is null = NullPointerException
Your try to acces element number 11 in a array of size 10... ArrayIndexOutOfBoundException
Encapsulation = One object is another one with but more evoluate. There is no link between overwritting method and Runtime Exception...


Alain Boucher<br />Spare-Brain Consultants Inc.<br />SCJ2P,SCWCD,
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

If you a given some code, and asked what kind of error it might generate, is there any way to figure it out?

Not sure what exactly you are asking here. If you are referring to exceptions, then the answer is a conditional yes. For example, you write some code that throws an exception of a particular type under certian circumstances. If you are refering to logical errors, though, then the answer is no: all you can do is to test often, extensively, and carefully.

What are the benefits of encapsulation?

Encapsulation promotes low coupling between the objects. The idea is to hide as much as possible of the class implementation detail, so that when you change the implementation of the class, it doesn't affect all other classes.

If the String class overrides equals(), then should any other method also be overriden to prevent an error?

What error? The reason the String class overrides equals() is not to prevent error, but to implement that method in a more specific way.
Eugene.
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
I know that a compile error does not allow a compile and a runtime error is an error caused by a faulty value that prevents execution.
I thought that if you looked at a program and something was not syntactically correct, then a compiler error would result. But if things were syntactically correct, but the arguments had the wrong values or something, then there would be a runtime error. An example is
int i=4;
int j=0;
int x=i/j;
This would give a runtime error and not a compiler error, correct?
But
double i=4.0;
double j=0.0;
double x=i/j;
would give no error, because of the fact that NaN is a legal construct as long as doubles are concerned.
But if I had a problem like
String s; //as a class variable not local
System.out.println(s);
would this give an error, and if so what kind.
How about if String s were a local variable, would it give an error?
And what about
int x[]=new int[10]; //array is a class variable
System.out.println(x[2]);
Would I get an error, and if so what kind? How about if the array were a local variable?
I came across this question somewhere, if String overrides equal(), then must it also override another method like Clone(), or finalize(), or compareto()?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Shashank Gokhale:
But if I had a problem like
String s; //as a class variable not local
System.out.println(s);
would this give an error, and if so what kind.
How about if String s were a local variable, would it give an error?
And what about
int x[]=new int[10]; //array is a class variable
System.out.println(x[2]);
Would I get an error, and if so what kind? How about if the array were a local variable?

Note that I haven't tried to compile these examples. I will just describe my own educated guesses.
For the first example, s is automatically initialized to null if it is a class member variable. This would cause a NullPointerException. However, if s is declared locally, then you will get a compiler error that it is not initialized.
The second example will give a compiler error because println() expects a String as its only argument. Since x is declared as an int[], the compiler will complain that it isn't a String.
HTH
Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime error vs compiler error