Calculations

Name Operator Examples Result
Addition + 1 + 3 4
2 + 4 6
"ab" + "cd" abcd
Subtraction - 5 - 3 2
2 - 2 0
3 - 5 -2
Multiplication * 2 * 2 4
3 * 5 15
3 * 0 0
Division / 10 / 2 5
0 / 3 0
5 / 0 Exception - cannot divide by 0.
Remainder Of Division % 10 % 2 0
4 % 3 1
17 % 6 5

Explanation for Remainder Of Division

division-remainder

Let's start with a program that reads width and height of a rectangle and prints it's area.

Input:

Output:

Explanation

  • At line 9 and 10 width and height are red.
  • Double.parseDouble is used to convert the string input to double.
  • At line 13 width and height are multiplied (via * operator).

Let's demonstrate the rest of the operators:

Input:

Output:

Notice that + operator can be applied to both numeric and string variables.