Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is it logical?

 
Mike Shn
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This and many other Java Gotchas are discussed briefly in Roedy Green's List of Java Gotchas.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic