Programming Tutorials and Interview Questions

Arithmetic operators +, -, *, /, and % perform addition, subtraction, multiplication, division, and modulo operations. Arithmetic operators can be applied on any numeric type: byte, short, int, long, float, or double. Java also provides unary plus (+) and unary minus (-) to make a numeric value positive or negative. Numeric values are by default positive, if they are not led by any sign. Following table lists Java's arithmetic operators.

Operator | Operation | Associativity |
---|---|---|

+ | Addition (Also used for string concatenation.) |
Left -> Right |

- | Subtraction | Left -> Right |

* | Multiplication | Left -> Right |

/ | Division | Left -> Right |

% | Modulo (remainder) | Left -> Right |

- | Unary Minus | Right -> Left |

The + operator adds two numbers. In addition to adding two numbers the + operator of Java is also used to concatenate two strings. Hence, if + is applied on two numeric quantities it performs arithmetic addition, and if it is applied on two strings it concatenates them together.

*Here, it is very important to note about the + operator is, if either operand of + is a string, the other one is converted to a string as well. Be sure to use parentheses when you want to combine addition with concatenation.*

The following piece of code demonstrates the string concatenation operation performed by `+`

operator.

/* OperatorDemo.java */ public class OperatorDemo { public static void main(String[] args) { int x = 10, y = 20; System.out.println("You may expect 30 but result is: " + x + y); System.out.println("To get 30, enclose x and y into brackets: " + (x + y)); } } OUTPUT ====== You may expect 30 but result is: 1020 To get 30, enclose x and y into brackets: 30

In above example the first print statement concatenates variables `x`

and `y`

with string `"You may expect 30 but result is: "`

then prints `"You may expect 30 but result is: 1020"`

as output. But in second print statement `x + y`

has been enclosed in brackets, hence the addition of `x`

and `y`

will take place first because brackets have higher precedence than +.

The minus operator (-) is used in two flavours: binary and unary minus. As a binary operator, it subtracts its second operand from its first. For example, 10 - 4 evaluates to 6. When the - operator is used in unary form it makes the number negative.

The multiplication operator (*) multiplies its two operands. For example, 9*3 evaluates to 27.

The division operator (/) divides its first operand by its second. If both operands are integers, the result is an integer, and any remainder is lost. If either operand is a floating-point value, however, the result is a floating-point value. When dividing two integers, division by zero throws an `ArithmeticException`

. For floating-point calculations, however, division by zero simply yields an infinite result or `NaN`

:

7/3 // Evaluates to 2 7/3.0f // Evaluates to 2.333333f 7/0 // Throws an ArithmeticException 7/0.0 // Evaluates to positive infinity 0.0/0.0 // Evaluates to NaN

The modulo operator (%) returns the remainder when the first operand is divided by the second operand an integral number of times. For example, 7%3 is 1. The sign of the result is the same as the sign of the first operand. In Java, modulo operator works on both integers and floating point values. For example, `32 % 5`

yields `2`

and `32.5 % 5`

yields `2.5`

. When operating with integers, trying to compute a value modulo zero causes an `ArithmeticException`

. When working with floating-point values, anything modulo 0.0 evaluates to `NaN`

, as does infinity modulo anything.

In C and C++ modulo operator works only on integers.

The following Java program demonstrates the use of modulo operator.

// Demonstrating modulus operator public class ModulusOperatorDemo { public static void main(String[] args) { int x = 32; float y = 32.5f; System.out.println("x modulus 5 is: " + (x % 5)); System.out.println("y modulus 5 is: " + (y % 5));; } } OUTPUT ====== x modulus 5 is: 2 y modulus 5 is: 2.5

This tutorial explained Java's arithmetic operators (addition, subtraction, multiplication, division, and modulo operators). Hope you have enjoyed reading this tutorial, please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

The BBCâ€™s technology correspondent Rory Cellan-Jones gets a beer poured for him by a robotic barman - but how long does it take?

US politicians have voted to let ISPs gather and sell data about what Americans do online.

Customers who suffer poor service could get automatic payouts under Ofcom's plan.