File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes is it logical? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "is it logical?" Watch "is it logical?" New topic

is it logical?

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
I don't see the logic at all behind the devision by 0 in Java.
Example # 1:
int a = 0;
int b = 2;
int c = b/a;
//Result is Exception in thread "main" java.lang.ArithmeticException: / by zero

Example #2:
int a = 0;
double b = 3.14;
double c =b/a;
IT'S very strange .
What do you think?
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 267
In the first case you are dealing with two integers which results in the exception.
In the second case you have a double so the integer gets promoted to a double (JLS 5.6.2). Not positive but I think the resulting infinity is to prevent program termination.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
This and many other Java Gotchas are discussed briefly in Roedy Green's List of Java Gotchas.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: is it logical?
It's not a secret anymore!