In Java, operators are symbols that are used to perform different operations on variables or values. Here are the different types of operators in Java:
Arithmetic Operators
These operators are used to perform mathematical operations on numeric values.
int a = 10;
int b = 20;
// Addition operator (+)
int c = a + b; // c = 30
// Subtraction operator (-)
int d = a - b; // d = -10
// Multiplication operator (*)
int e = a * b; // e = 200
// Division operator (/)
int f = b / a; // f = 2
// Modulus operator (%)
int g = b % a; // g = 0
Relational Operators
These operators are used to compare values and return a boolean value (true or false).
int a = 10;
int b = 20;
// Greater than operator (>)
boolean c = b > a; // c = true
// Less than operator (<)
boolean d = a < b; // d = true
// Greater than or equal to operator (>=)
boolean e = b >= a; // e = true
// Less than or equal to operator (<=)
boolean f = a <= b; // f = true
// Equal to operator (==)
boolean g = a == b; // g = false
// Not equal to operator (!=)
boolean h = a != b; // h = true
Logical Operators
These operators are used to combine boolean expressions and return a boolean value.
boolean a = true;
boolean b = false;
// Logical AND operator (&&)
boolean c = a && b; // c = false
// Logical OR operator (||)
boolean d = a || b; // d = true
// Logical NOT operator (!)
boolean e = !a; // e = false
Assignment Operators
These operators are used to assign values to variables.
int a = 10;
// Simple assignment operator (=)
a = 20; // a = 20
// Addition assignment operator (+=)
a += 5; // a = 25
// Subtraction assignment operator (-=)
a -= 10; // a = 15
// Multiplication assignment operator (*=)
a *= 2; // a = 30
// Division assignment operator (/=)
a /= 3; // a = 10
// Modulus assignment operator (%=)
a %= 3; // a = 1
These are the different types of operators in Java. Understanding operators is essential to writing Java programs, as they are used extensively in expressions and statements.