Java Math Operators & Special Operators

  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.

--

--

--

Software Developer, Java Instructor https://www.techleadacademy.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Wearables and HRV monitor my stress, but digital stress balls don’t work for me. Help!

Create and deploy your own COVID-19 tracker in 3 days !

Homepage

DojoToken 5% Commission Referral Program

General Tree Implementation

Embracing functional style within object-oriented paradigm

GC Deep Dive — Matter DeFi

Running a flask app in a docker containers with VS Code docker extension

Dynamic Scheduling with Spring Boot

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beknazar

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

More from Medium

Understanding NULL in Java

Java Lesson 19: Fun with Dates and Times (in Java)

Basics of Java Programming

Unit Testing in Java