Java Math Operators & Special Operators

Beknazar
4 min readFeb 25, 2021

--

  1. Addition.
  2. Subtraction.
  3. Multiplication
  4. Division
  5. Remainder
  6. Special operators

Addition

We perform addition by using + operator on numeric data types

int num1 = 5;
int num2 = 6;
int sum = num1 + num2;
System.out.println(sum); // 11
int num3 = sum + 10;
System.out.println(num3); // 21
num3 = num3 + 1;
System.out.println(num3); // 22

Subtraction

We perform subtraction by using operator on numeric data types

int num1 = 5;
int num2 = 6;
int sum = num2 - num1;
System.out.println(sum); // 1
int num3 = 100 - sum;
System.out.println(num3); // 99
num3 = num3 - 10;
System.out.println(num3); // 89

Multiplication

We perform multiplication by using * operator on numeric data types

int num1 = 5;
int num2 = 6;
int sum = num1 * num2;
System.out.println(sum); // 30
sum = sum * 2;
System.out.println(sum); // 60

Division

We perform division by using / operator on numeric data types

int num1 = 50;
int num2 = 10;
int sum = num1 / num2;
System.out.println(sum); // 5
// Sometimes when dividing we can get a floating numberint num3 = 5 / 2;
System.out.println(num3); // 2
// Actual result is 2.5 but we should always remember that int is only for whole numbers so it will be 2.
int num4 = 8 / 5;
double num5 = 8.0 / 5;
System.out.println(num4); // 1
System.out.println(num5); // 1.6
// remember it will not round, it will always truncate

Remainder

The remainder operator will calculate what remains after division. % — this is the remainder.

System.out.println(12 % 10); // 2 because 12 / 10 is 1 and 2 remains
System.out.println(12 % 5); // 2 because 12 / 5 is 2and 2 remains
System.out.println(10 % 5); // 0
System.out.println(107 % 10); // 7
System.out.println(9 % 4); // 1
System.out.println(119 % 100); // 19
System.out.println(25 % 10); // 5
System.out.println(12 % 3); // 0

Special operators

Regular:

int i = 5;
i = i + 2;
System.out.println(i); // 7

With special operator:

int i = 5;
i += 2;
System.out.println(i); // 7

Both above examples are doing the exact same thing — adding 2 to our i variable.

Regular:

int i2 = 10;
i2 = i2 - 2;
System.out.println(i2); // 8
int i3 = 4;
i3 = i3 * 3;
System.out.println(i3); // 12
int i4 = 10;
i4 = i4 / 2;
System.out.println(i4); // 5

With special operator:

int i2 = 10;
i2 -= 2;
System.out.println(i2); // 8
int i3 = 4;
i3 *= 3;
System.out.println(i3); // 12
int i4 = 10;
i4 /= 2;
System.out.println(i4); // 5

Increment and Decrement operators

The increment operator will increase the value of a numeric variable by 1.

int num = 10;
num++;
System.out.println(num); // 11
System.out.println(num++); // 11
System.out.println(num); // 12
System.out.println(++num); // 13
  • post-increment (++ after variable) will increase value in the next use of the variable.
  • pre-increment will increase value immediately.

The decrement operator will decrease the value of a numeric variable by 1.

int num = 10;
num--;
System.out.println(num); // 9
System.out.println(num--); // 9
System.out.println(num); // 8
System.out.println(--num); // 7
  • post-decrement(after variable) will decrease the value the next time we use our variable.
  • pre-decrement will decrease value immediately.

--

--

Beknazar
Beknazar

No responses yet