## Arithmetic and Assignment Operators Explained in Java

Originally posted on makeuseof.

We promise these Arithmetic and Assignment Operators are more fun than the ones you used in Algebra II.

Arithmetic operators allow you to perform algebraic arithmetic in programming. That is, they enable you to add, subtract, divide and multiply numbers.

This article will also cover assignment operators. These enable you to give (assign) a certain value to a variable.

This tutorial is not just for Java programmers. Many other programming languages like C and Python use these same operators. Therefore, you can easily transfer and apply the knowledge you gain here.

## Arithmetic Operators

There are 5 arithmetic operators in Java—the table below summarizes them.

Operator Name Symbol Sample Expression
Subtraction y-8
Multiplication * x*y
Division / x/2
Remainder % y%3

The symbols (+, –/) should seem familiar. That’s because they’re the same as those typically used in algebra.

It’s important to take note that the division operator (/) refers to integer division here. That is, 19/5 will evaluate to 3. Any fractional part that results from this computation is truncated.

You should have also noticed that the Java operator for multiplication is an asterisk (*) and not the usual multiplication symbol (×).

To get the modulus of two integers, Java uses the % symbol. The example given in the table is similar to the algebraic expression: y mod 3. The % operator gives the remainder after y is divided by 3. That is, 19%5 will evaluate to 4.

It’s good practice to use parentheses for grouping subexpressions. This eases readability and helps to avoid logic and syntax errors.

``( 4*y+(z/3)) // example``

When you have multiple arithmetic operators in one expression, Java uses the rules of operator precedence to determine which subexpressions to evaluate first.

The table below categorizes the levels of operator precedence.

Precedence Operator Description
1 *
/
%
Multiplication, division and modulus have the same level of precedence. If there are multiple operators of this type used, they are evaluated from left to right.
2 +
Addition and subtraction have the same level of precedence. If there are multiple operators of this type used, they are evaluated from left to right.
3 = This operator is evaluated last .

The operators (*/%) have the highest level of precedence, then followed by (+) and finally (=). The operators (*/%), and (+) all associate from left to right. This simply means that their evaluation begins from the leftmost operator.

The third operator (=) associates from right to left. So if have x=3, that means 3 is assigned to x, and not x is assigned to 3.

## Assignment Operators

The assignment operator (=) assigns a value to a variable.

``y = y+7;``

The above expression adds 7 to y and then assigns the final result to y. If you’re new to programming, this expression might seem a little weird. This shouldn’t bother you as the compiler will understand what you’re trying to do.

### Compound Assignment

You can simplify the way you express an assignment by using a compound assignment operator.

In the previous example, we could’ve simply written:

``y+=7;``

See the table below on how you can use compound assignment operators.

Compound Operator Sample Expression Expanded Form
+= x+=2 x=x+2
-= y -=6 y=y-6
*= z *=7 z=z*7
/= a /=4 a=a/4
%= b %=9 b= b%9

### Increment & Decrement Operators

If you have the compound assignment +=1, you can simply write it as ++. This is known as the “increment operator”. Similarly, the decrement operator is .

When used before the operand, the increment and decrement operators are known as “prefix operators”. And when used after the operand, they’re called “postfix operators”.

With prefix, the variable being operated on is first modified and then used while with postfix, the initial value before modification is used.

``````y++; //postfix, most preferred form by many programmers
++y; // prefix``````

Generally, both postfix and prefix operators yield the same answer. It’s only when dealing with large expressions that the answer may change.

## Make Operators Work For You

It’s important to note that increment and decrement operators only act on variables (e.g. x++) and not direct values (but not 5++). You should also not leave any whitespace while using increment and decrement operators, unlike with the operators before that. Doing so will give a compile-time error.

Always use parentheses when possible to logically group expressions. This will avoid unnecessary logic errors.

With these operators under your belt, understanding how to use access modifiers in Java will be a piece of cake.

Source: makeuseof