In this lesson, we will look at the Java Operators. Operators can be defines as the most fundamental unit of any programming language. It can be used for performing multiple operations on different values and variables. It also provides multiple group of operators. These are symbols that holds the mathematical operations. There are different types of operators based upon their values.
We will be walking around different Java operators in this tutorial and learning about their functionalities and how to use them in depth.
Operators can be divided into multiple categories based upon their usage and functionalities. Following are some types of operators in Java that are defined below:
This type of operators defines the arithmetic operation to perform simple mathematical operations. These types of operators only works with the primitive value types and their boxed types such as int and Integer, long and Long. Let’s check the different types operators present in Arithmetic Operators:
Operator | Name | Description | Example |
+ | Addition | This is used for adding two values. | 2+3=5 |
– | Subtraction | This is used for subtracting one value from another. | 3-2=1 |
* | Multiplication | This is used for multiplication of two values. | 2*3=6 |
/ | Division | This divides one value by another. | 3/2 = 1.5 |
% | Modulus | This returns the remainder after division | 3%2 = 1 |
++ | Increment | This increases the value of a variable by 1. | x++ = 4(where x is 3) |
— | Decrement | This decreases the value of a variable by 1. | x– = 2(where x is 3) |
Let’s see how to implement the arithmetic operators in any java program.
public void arithmeticOperators() {
// Addition
int eight = 4 + 4;
String title = "java" + "dev" + "journal";
// Subtraction
int three = 10 - 7;
int negativeResponse = 7– 10;
// Multiplication
int hundred = 20 * 5;
int fifteen = -5 * -3;
// Division
int two = 10 / 5;
int seven = 15 / 2;
// Modulo
int one = 13 % 2;
int zero = 12 % 5;
}
The Assignment Operators are used for assigning values into variables. There are mainly two types of assignment operators in java i.e.
It is a straightforward operator that is used for assigning values into variables. Equal symbol (=)
represents assignment operators in java. It assigns the value on its right to the operand on its left.
public void arithmeticOperators() {
// Assigning values
int assignFour = 4;
String title = "Hello User";
}
The compound assignments are basically used for assigning the combined value of arithmetic operators with the single assignment operator. For Instance, writing “a = a + 2
” in a compound way converts to: “a += 2
”. Let’s check some supported compound assignments in Java through below examples.
public void assignmentOperators() {
int a, b, c, d, e = 10
a += 5; // a = 15, same as a = a + 5
b -= 5; // b = 5, same as b = b - 5
c *= 5; // c = 50, same as c = c * 5
d /= 5; // d = 2, same as d = d / 5
e %= 5; // e = 0, same as e = e % 5
}
These are comparison operators that are used for making the comparison among values. We can compare both the values and the variables using these operators.
We can use the double equals symbol (==)
for comparing two values and check if both the values equate to each other or not. This will return true if both the values or variables are equal to each other.
public void relationalOperators() {
int number1 = 5; // Assigning values
int number2 = 5;
boolean theyAreEqual = number1 == number2; // returns true
}
This operator is used for checking the opposite of the equal operator i.e. (!=)
. This returns TRUE
when both the values are not equal.
This compares two value with the less than operator (<)
that returns true when the value on left hand side is less than the value on right hand side.
public void relationalOperators() {
int number1 = 7;
int number2 = 5;
boolean notEqual = number1 != number2; // 7 != 5
boolean greater = number1 > number2; // 7 > 5
boolean lesser = number2 < number1; // 5 < 7
}
This compares two value with the greater than operator (>)
that returns true when the values on left hand side is greater than the value on right hand side.
Java mainly has two logical operators, i.e. Logical AND
and OR
operators. Their function basically helps the user to achieve specific operations. A Logical operator is mostly used with two operands which are variables or define the expressions that can be evaluated as boolean. Let’s dive deep into these logical operators.
The Logical AND (&&)
operator returns true when both the operands are true. The Logical AND
operator performs the specific operation:
public void logicalOperators() {
int number1 = 7;
int number2 = 5;
boolean andTrue = (number1 > 0) && true; // true
boolean andFalse = (number1 > 0) && false; // false
boolean notEqual = (number1 > 0) && (number1 > number2); //true
}