Java Math Operators and special operators

Photo by Chris Liverani on Unsplash
  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 statement.
  • 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 value in the next statement.
  • pre-decrement will decrease value immediately.
This article is part of the series of articles to learn Java programming language from Tech Lead Academy:1. Introduction to programming 
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

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

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