jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Division & Modulus Operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Division & Modulus Operators" Watch "Division & Modulus Operators" New topic

Division & Modulus Operators

geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
Pl.clarify if the following are correct.
1.Integral division by 0 :Runtime Exception
2.Floating point division by 0:Result is Infinity
1.Integral Modulus operation by 0:Runtime Exception
(int i =10;
int result = i % 0
2.Floating point Modulus operation by 0:Result is Nan.
Jeff Smith
Ranch Hand

Joined: May 04, 2001
Posts: 50
1.] Yes, divide by zero throws an ArithmeticException, which is a RuntimeException
2.] Floating point division by zero throws an ArithmeticException, results are infinite
3.] Throws ArithmeticException, divide by zero.
4.] Throws ArithmeticException, result is not a number (NaN).
Here's a test class so you can see exactly this:
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Geetha,
That's about the size of it. Just remember that if you do something like:

You'll still get a RuntimeException because the expression on the right evaluates to an int. So you need to do something like this instead:

Hope this helps
Michael Morris

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
I agree. Here's the link: http://aspose.com/file-tools
subject: Division & Modulus Operators
It's not a secret anymore!