# Arithmetic Expressions

index
 BASIC

An arithmetic expression is an expression that results in a numeric value. There are two kinds of numeric values, integers (whole numbers), and real or floating point numbers (numbers containing a decimal point).

The simplest arithmetic expressions are literals (the number itself, written with digits) and variables (named values):

Example Description
`12` A literal integer, representing the number 12.
`-5` A literal integer, representing the number negative 5.
`-5.0` A literal real, representing the number negative 5.
`5.0E4` A literal real, representing the number 50000.
`count` A variable. If it was declared as `int count`, it will hold an integer value; but if declared as `double count`, it will hold a real value.

More complex arithmetic expressions can be formed by connecting literals and variables with one of the arithmetic operators:

Operator Meaning
`+` Add.
`-` Subtract.
`*` Multiply (it's difficult to type the usual multiplication symbol).
`/` Divide (it's difficult to type the usual division symbol).
Division of two integer values will give an integer result (any fractional part is just discarded). For example, `14/5` gives `2`. This is called integer division.
`%` Mod (remainder).
Used for integers only, this operation gives the remainder of a division; for example, `14%5` gives `4`. The sign (positive or negative) of the result is the same as the sign of the first number.

Parentheses may be used to control the order in which the operators are applied. If you don't use parentheses, operations with higher precedence are done first.

Notes:

• If you mix integers and reals in an operation, the result is a real. For example, `3*5.0` is `15.0`, not `15`.
• You can assign an integer value to a real variable. For example, `double x=5` sets `x` to `5.0`.
• You cannot assign a real value to an integer variable. For example, both``` int i=5.8``` and` int i=5.0` are illegal. This is to protect you from accidentally losing the fractional part.However, if you use a cast to reassure Java that you really mean it, then it's legal. For example, `int i=(int)5.8` is legal,and gives` i` the value `5`.
• By far the most commonly used numeric types are `int` and `double`. However, there are other numeric primitive types.