Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Division by Zero confusion

 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
My understanding about the literals being assigned to premitive types was that they are being evaluated during the compile time.
For example if we say
byte b1=130;
We will get a compiler error.
But why this is not true with
int i1=5/0;
When i compile this code it's getting compiled and it gives only run time exception.
I remember seeing int i1=5/0 kind of statements would generate compile time error even on "The Rules Round-up Game".
One possible explanation that i could give to my self is Division by zero belongs to the hierarchy of Run time exceptions.
But i can't understand when he compiler checks whether 130 can be assigned to a byte or not why it's not checking the value of 5/0?
Please help.
 
Bojan Knezovic
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there certainly are some things that don't make too much sense around divide by 0. For example this

won't even throw an exception. It'll just return Infinity.
 
jay foster
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sekhar,
The reason that the compiler will not object to "int i1=5/0;" is that there is an operator involved that can only be executed at run-time.
To my knowledge, the only time instructions are executed at compile time is when specifying a value for a case statement since case statements must have their values known at compile time. This is why they must be final or literal values or a combination of both.
Ex:
final int a = 5; // if a were not final the compiler would not be able to guess the value!
switch(b)
{
case a - 5: // actually computed at compile time to get value 0
....
}
 
Ritesh Agrawal
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sekhar,
Exceptions can be broadly classified into two types, viz. Checked Exceptions and Unchecked Exceptions. Checked Exceptions are those which are checked during the compile time and you are required to mention those in your method declarations' throws clause. Unchecked are the Runtime Exceptions which are not caught by compiler. Now when you code something like
int i = 5 / 0 ;
During compilation, compiler just checks to see whether the assignment is correct or not. 5 and 0 are both valid integers and so the operation / on 5 and 0 would yield and int value only. So there would be any compile time error. Now Divide By Zero is an ArithmeticException, which is actually a subclass of RuntimeException, i.e unchecked, so compiler doesn't bother to check that. It's the duty of the programmer to avoid such exceptions. So, the code compiles without error.
Again, lets consider the second piece of code you have mentioned,
byte b = 130;
Here, during compilation, compiler knows the type of b, i.e byte, and that it can hold a maximum value of 127. So, during compilation itself it realizes that the programmer is making a mistake and it becomes compiler's duty to inform the programmer, that he/she might be wrong. If you don't want to pay any heed to compiler's advice and go ahead by changing the code to
byte b = (byte) 130;
The compiler compiles your code without complaining. It assumes, the programmer is smart enough and knows what he is doing.
So, the explanation that you gave yourself was pretty much good.
[ April 02, 2004: Message edited by: Ritesh Agrawal ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic