Java Operators

 

Operators are used to perform actions or operations on variables and values.

int sum = 2 + 5;

2 and 5 are values also called operands.
+ is called an operator

Operand Operator Operand
2 + 5

Here are the groups of operators:
a. Arithmetic Operators
b. Comparison Operators
c. Logical Operators
d. Assignment Operators
e. Ternary Operator

Arithmetic Operators
Arithmetic operators are used to perform mathematical operations just like algebra math you have in school.

Operator Description Example
+ (Addition) Adds values. 5 + 5 will give 10
– (Subtraction) Subtracts values. 10 – 5 will give -5
* (Multiplication) Multiplies values. 5 * 4 will give 20
/ (Division) Divides left-hand operand by right-hand operand. 4 / 2 will give 2
% (Modulus) Returns remainder of division. 0 % 2 will give 0
++ (Increment) Increases the value by 1. 4++ gives 5
— (Decrement) Decreases the value by 1. 4– gives 3

Comparison Operators
Comparison operators are used to compare two or more values.

Operator Description Example
== (equal to) Compares if the two values are equal or not (1 == 2) is not true.
!= (not equal to) Compares if the two values are not equal (1 != 2) is true.
> (greater than) Compares if the value on the left-hand side is greater than the value on the right-hand side. (1 > 2) is not true.
< (less than) Compares if the value on the left-hand side is less than the value on the right-hand side. (1 < 2) is true.
>= (greater than or equal to) Compares if the value on the left-hand side is greater than or equal to the value on the right-hand side. (1 >= 2) is not true.
<= (less than or equal to) Compares if the value on the left-hand side is less than or equal to the value on the right-hand side. (1 <= 2) is true.

Assignment Operators
Assignment operators are used to assign values to variables

Operator Description Example
= Assigns values from right side operands to left side operand. age = 2 + 4 will assign value of 2 + 3 into age
+= Adds right value to the left operand and assign the result to left operand. age += 2 is equivalent to age = age + 2
-= Subtracts right value from the left operand and assign the result to left operand. age -= 2 is equivalent to age = age – 2
*= Multiplies right value with the left operand and assign the result to left operand. age *= 2 is equivalent to age = age * 2
/= Divides left value with the right operand and assign the result to left operand. age /= 4 is equivalent to age = age / 4
%= Takes modulus using two operands and assign the result to left operand. age %= 4 is equivalent to age = age % 4


Ternary Operator
Ternary operator is used to evaluate a boolean expression.
variable = (condition) ? (execute if condition is true) ? (execute if condition is false);

String action = (1 > 5) ? "fire" : "water";
// action will be "water" because 1 is not greater 5

Instanceof Operator
Instanceof operator is used to check the type of a variable. This is a very useful operator when working with objects and classes.

String name = "Folau";
boolean isAString = name instanceof String;
// isAString here is true because name is of data type String.

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.