• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime error vs compiler error

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Alain Boucher
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
John Smith
Ranch Hand
Posts: 2937
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic