aspose file tools*
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
Author

is it logical?

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Hello
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;
//Result
Infinity
IT'S very strange .
What do you think?
Thanks
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 259
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
Sheriff

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: http://aspose.com/file-tools
 
subject: is it logical?