hts.StevenWood.com

[ Last Page ]

The primary math operators (+, -, /, *, %)
(these work on integers and floats/doubles
) 

There are five mathematical operators:

Operator Meaning Example  

+

Addition 4 + 5 Multiplication, addition and subtraction work as you would expect.

-

Subtraction 7 - 2

*

Multiplication 12 * 6

/

Division 48 / 12

Division with doubles gives you a decimal answer, e.g. 25.0 / 4.0 = 6.25

Integer division is a little different from everyday division because of the fact that computers can lose information due to precision constraints. For example, when you divide 27 by 5, the result is a real number (a number with a decimal component). Integers don't have a decimal component (or a fraction component) and so the "remainder" is discarded.

The division of  two integers like 27 divided by 5 will  result in an answer of 5. The ".4" gets discarded.

%

Modulus 12 % 5 The modulus operator tells you the remainder after an integer division. To find the remainder of the division of  two integers like 27 divided by 5, you would use the modulus division operator %.You would do this by  calculating  27 modulus 5 (27 % 5) and you would get the result 2.

 

Source: http://www.tvdsb.on.ca/banting/ICS3U/unit3/arithmetic_in_java.htm

[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+