05.Java Operators
05.Java Operators
In this tutorial, you'll learn about different types of operators in Java, their
syntax and how to use them with the help of examples.
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
a + b;
Here, the + operator is used to add two variables a and b . Similarly, there
are various other arithmetic operators in Java.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));
}
}
Run Code
Output
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
In the above example, we have used + , - , and * operators to compute
addition, subtraction, and multiplication operations.
/ Division Operator
Note the operation, a / b in our program. The / operator is the division
operator.
If we use the division operator with two integers, then the resulting quotient
will also be an integer. And, if one of the operands is a floating-point
number, we will get the result will also be in floating-point.
In Java,
(9 / 2) is 4
(9.0 / 2) is 4.5
(9 / 2.0) is 4.5
(9.0 / 2.0) is 4.5
% Modulo Operator
The modulo operator % computes the remainder. When a = 7 is divided
by b = 4, the remainder is 3.
int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the
variable on its left. That is, 5 is assigned to the variable age .
Let's see some more assignment operators available in Java.
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
// create variables
int a = 4;
int var;
Output
var using =: 4
var using +=: 8
var using *=: 32
3. Java Relational Operators
Relational operators are used to check the relationship between two
operands. For example,
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b); // false
// != operator
System.out.println(a != b); // true
// > operator
System.out.println(a > b); // false
// < operator
System.out.println(a < b); // true
// >= operator
System.out.println(a >= b); // false
// <= operator
System.out.println(a <= b); // true
}
}
Run Code
! (Logical
!expression true if expression is false and vice versa
NOT)
// && operator
System.out.println((5 > 3) && (8 > 5)); // true
System.out.println((5 > 3) && (8 < 5)); // false
// || operator
System.out.println((5 < 3) || (8 > 5)); // true
System.out.println((5 > 3) || (8 < 5)); // true
System.out.println((5 < 3) || (8 < 5)); // false
// ! operator
System.out.println(!(5 == 3)); // true
System.out.println(!(5 > 3)); // false
}
}
Run Code
Working of Program
• (5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true .
• (5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false .
• (5 < 3) || (8 < 5) returns false because both (5 < 3) and (8 < 5) are false .
Operator Meaning
+ Unary plus: not necessary to use since numbers are positive without using it
int num = 5;
// increase num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.
// declare variables
int a = 12, b = 12;
int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
Run Code
Output
Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
~ 00100011
________
11011100 = 220 (In decimal)
Here, ~ is a bitwise operator. It inverts the value of each bit
(0 to 1 and 1 to 0).
The various bitwise operators present in Java are:
Operator Description
~ Bitwise Complement
^ Bitwise exclusive OR
These operators are not generally used in Java. To learn more, visit Java
Bitwise and Bit Shift Operators.
Other operators
Besides these operators, there are other additional operators in Java.
Output
Here, str is an instance of the String class. Hence, the instanceof operator
returns true . To learn more, visit Java instanceof.
class Java {
public static void main(String[] args) {
// ternary operator
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result);
}
}
Run Code
Output
Leap year
In the above example, we have used the ternary operator to check if the
year is a leap year or not. To learn more, visit the Java ternary operator.
Now that you know about Java operators, it's time to know about the order
in which operators are evaluated. To learn more, visit Java Operator
Precedence.