- Addition.
- Subtraction.
- Multiplication
- Division
- Remainder
- 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); // 11int num3 = sum + 10;
System.out.println(num3); // 21num3 = 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); // 1int num3 = 100 - sum;
System.out.println(num3); // 99num3 = 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); // 30sum = 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); // 8int i3 = 4;
i3 = i3 * 3;
System.out.println(i3); // 12int i4 = 10;
i4 = i4 / 2;
System.out.println(i4); // 5
With special operator:
int i2 = 10;
i2 -= 2;
System.out.println(i2); // 8int i3 = 4;
i3 *= 3;
System.out.println(i3); // 12int 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); // 11System.out.println(num++); // 11
System.out.println(num); // 12System.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); // 9System.out.println(num--); // 9
System.out.println(num); // 8System.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.
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET