aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Division by Zero confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Division by Zero confusion" Watch "Division by Zero confusion" New topic
Author

Division by Zero confusion

Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
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.


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
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

Joined: Apr 02, 2004
Posts: 2
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

Joined: Jan 08, 2004
Posts: 74
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 ]

Ritesh<br /> <br />SCJP 1.4<br />IBM Test 340<br />IBM AIX V4.0 Certified Professional<br /> <br />Right actions for the future are the best apologies for wrong ones in the past.<br />- Tyron Edwards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Division by Zero confusion