Java operators
Operator in java is a symbol that is used to perform some operation.
Example: +
, -
, *
, /
etc.
Java operators has been classified as following categories
Unary Operator
The unary operators works on only one operand, they perform various operations such as incrementing/decrementing
a value by one, negating an expression, or inverting the value of a boolean.
Pre increment operator
This operator increments
the value by one before its evaluation in the expression.
Example:
- int x=5;
- System.out.println(++x); // output will be 6
int x=5; System.out.println(++x); // output will be 6
Pre decrement operator
This operator decrements
the value by one before its evaluation in the expression.
Example:
- int x=5;
- System.out.println(--x); // output will be 4
int x=5; System.out.println(--x); // output will be 4
Post increment operator
This operator increments
the value by one after its evaluation in the expression.
Example:
- int x=5;
- System.out.println(x++); // output will be 5
- System.out.println(x); // output will be 6 as its incremented after above line execution
int x=5; System.out.println(x++); // output will be 5 System.out.println(x); // output will be 6 as its incremented after above line execution
Post decrement operator
This operator decrements
the value by one after its evaluation in the expression.
Example:
- int x=5;
- System.out.println(x--); // output will be 5
- System.out.println(x); // output will be 4 as its decremented after above line execution
int x=5; System.out.println(x--); // output will be 5 System.out.println(x); // output will be 4 as its decremented after above line execution
Negation operator
It’s used to invert
the Boolean value from true to false or false to true.
- boolean success = false;
- System.out.println(success); output will be false
- System.out.println(!success); output will be true
boolean success = false; System.out.println(success); output will be false System.out.println(!success); output will be true
Arithmetic Operator
Arithmetic operators are used to perform basic arithmetic operations which are explained as below
Addition
It’s used to add
the numbers or concatenation of strings
Example:
- int num1=20;
- int num2=10;
- int result = num1+num2;
- System.out.println(result); output will be 30
int num1=20; int num2=10; int result = num1+num2; System.out.println(result); output will be 30
Subtraction
It’s used to subtract
the numbers
Example:
- int num1=20;
- int num2=10;
- int result = num1-num2;
- System.out.println(result); output will be 10
int num1=20; int num2=10; int result = num1-num2; System.out.println(result); output will be 10
Multiplication
It’s used to multiply
the numbers
Example:
- int num1=20;
- int num2=10;
- int result = num1*num2;
- System.out.println(result); output will be 200
int num1=20; int num2=10; int result = num1*num2; System.out.println(result); output will be 200
Division
It’s used to perform division
among the numbers
Example:
- int num1=20;
- int num2=10;
- int result = num1/num2;
- System.out.println(result); output will be 2
int num1=20; int num2=10; int result = num1/num2; System.out.println(result); output will be 2
Remainder Finder
It’s used to divide
one number by another and returns the remainder
as it’s result
Example:
- int num1=20;
- int num2=6;
- int result = num1%num2;
- System.out.println(result); output will be 2
int num1=20; int num2=6; int result = num1%num2; System.out.println(result); output will be 2
We know that when we divide 20 by 6 , we get reminder as 2.
Note
The+
operator can also be used for concatenating (joining) two strings together
- String firstString = "This is";
- String secondString = " a concatenated string.";
- String thirdString = firstString+secondString;
- System.out.println(thirdString); // output will be “This is a concatenated string”
String firstString = "This is"; String secondString = " a concatenated string."; String thirdString = firstString+secondString; System.out.println(thirdString); // output will be “This is a concatenated string”
Relational Operator
Java has 6 relational operators that compare two operands and return a boolean value.
Let us say x
and y
are the 2 operands
x < y
is true if x is less than y, false otherwise
x > y
is true if x is greater than y, false otherwise.
x <= y
is true if x is less than or equal to y, false otherwise.
x >= y
is true if x is greater than or equal to y, false otherwise.
x == y
is true if x equals y, false otherwise.
x != y
is true if x is not equal to y, false otherwise.
Example
- boolean res1 = 10 < 20; // true. As 10 is less than 20
- Boolean res2 = 10 > 20; // false. As 10 is not greater than 20
- boolean res3 = 10 != 20; // true. As 10 does not equal to 20
- boolean res4 = 10 >= 20; //false. As 10 is neither equal to 20 nor greater than 20
- boolean res5 = 10 <= 20; // true. As 10 is less than 20
- boolean res6 = 10 == 10; // true. As 10 is equal to 10
boolean res1 = 10 < 20; // true. As 10 is less than 20 Boolean res2 = 10 > 20; // false. As 10 is not greater than 20 boolean res3 = 10 != 20; // true. As 10 does not equal to 20 boolean res4 = 10 >= 20; //false. As 10 is neither equal to 20 nor greater than 20 boolean res5 = 10 <= 20; // true. As 10 is less than 20 boolean res6 = 10 == 10; // true. As 10 is equal to 10
Generally, We use these conditions to check certain business condition and decide the flow based on that Boolean value.
Example
- int age ;
- If(age >= 18){
- System.out.println(“Eligible for voting”);
- }
- Else{
- System.out.println(“Not eligible for voting”);
- }
int age ; If(age >= 18){ System.out.println(“Eligible for voting”); } Else{ System.out.println(“Not eligible for voting”); }
Logical operators
Logical AND
This operator checks 2 operands
If both the operands are true then only it returns true.
If any one of them is false, then it returns false.
If first operand is false, then it won’t evaluate second operand,rather it returns whole result as false.
Example:
- int a=20;
- int b=10;
- int c=30;
- System.out.println(a<b&&a<c);//false && true and hence result is false
int a=20; int b=10; int c=30; System.out.println(a<b&&a<c);//false && true and hence result is false
- int a=5;
- int b=10;
- int c=30;
- System.out.println(a<b&&a<c);//true && true and hence result is true
int a=5; int b=10; int c=30; System.out.println(a<b&&a<c);//true && true and hence result is true
Logical OR
This operator checks 2 operands
If any one of the operands is true then it returns true.
If both of them are false, then it returns false.
If first operand is true, then it won’t evaluate second operand,rather it returns whole result as true.
Example:
- int a=20;
- int b=10;
- int c=30;
- System.out.println(a<b || a<c);//false || true and hence result is true
int a=20; int b=10; int c=30; System.out.println(a<b || a<c);//false || true and hence result is true
- int a=100;
- int b=50;
- int c=30;
- System.out.println(a<b || b<c);//false || false and hence result is false
int a=100; int b=50; int c=30; System.out.println(a<b || b<c);//false || false and hence result is false
Logical NOT
This operator is a unary logical operator which will just negate the boolean result.
Returns true if the operand to the right evaluates to false.
Returns false if the operand to the right is true.
Example:
- boolean a=true;
- !a returns false
boolean a=true; !a returns false
- boolean a=false;
- !a returns true
boolean a=false; !a returns true
Practical use of Logical NOT is as below
- Public Boolean isEligibleForVote(Person p){
- If(p.age >= 18){
- return true;
- }
- }
- If(!isEligibleForVote(p)){
- System.out.println(“You are not eligible for voting”);
- }
Public Boolean isEligibleForVote(Person p){ If(p.age >= 18){ return true; } } If(!isEligibleForVote(p)){ System.out.println(“You are not eligible for voting”); }
Bitwise operators
Bitwise Shift operators are used to change individual bits in an operand
Let’s see the below table to understand the results of bitwise &,| and ^
O can be treated as false
1 can be treated as true
Bitwise " & "
returns true if both operands are true
Bitwise " | "
returns true if any one of the operands is true
Bitwise " ^ "
returns false if both the operands are true or both the operands are false.
Difference between Bitwise and Logical operators
Bitwise &
and Logical &&
Bitwise &
checks both the operands no matter whether first operand is true or false
Logical &&
checks second operand only if first operand is true.
If first operand is false
then it won’t evaluate second operand instead returns the result as false
.
Bitwise |
and Logical ||
Bitwise |
checks both the operands no matter whether first operand is true or false
Logical |
checks second operand only if first operand is false.
If first operand is true
then it won’t evaluate second operand instead returns the result as true
Bitwise shift operators
The bitwise shift operators shifts the bit
value.
The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted.
Example
- a=0000100
- b=3
- a<<b left shifts the value in ‘a’ by 3 positions
- a<<b = 0100000
- a>>b right shifts the value in ‘a’ by 3 positions
- a>>b = 1000000
a=0000100 b=3 a<<b left shifts the value in ‘a’ by 3 positions a<<b = 0100000 a>>b right shifts the value in ‘a’ by 3 positions a>>b = 1000000
Assignment operators
Examples
- int x=20;
- int y=30;
- x+=y;//x=x+y (x=20+30 and hence x=50)
- System.out.println(x);
- System.out.println(y);
int x=20; int y=30; x+=y;//x=x+y (x=20+30 and hence x=50) System.out.println(x); System.out.println(y);
- int x=20;
- int y=30;
- x-=y;//x=x-y (x=20-30 and hence x=-10)
- System.out.println(x);
- System.out.println(y);
int x=20; int y=30; x-=y;//x=x-y (x=20-30 and hence x=-10) System.out.println(x); System.out.println(y);
Conditional operators
Conditional operator is represented by ? : which is a shorthand for if then else statement
This operator is also known as the ternary operator because it uses three operands.
Syntax :
- result = someCondition ? value1 : value2;
result = someCondition ? value1 : value2;
If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result.
Example
- int x=10;
- int y=20;
- int max=(x>y)?x:y;
- System.out.println(“Max value is “+max);
int x=10; int y=20; int max=(x>y)?x:y; System.out.println(“Max value is “+max);
Same condition can be written using if else as below
- int x=10;
- int y=20;
- int max;
- If(x>y){
- Max=x;
- }
- Else{
- Max=y;
- }
- System.out.println(“Max value is “+max);
int x=10; int y=20; int max; If(x>y){ Max=x; } Else{ Max=y; } System.out.println(“Max value is “+max);
Instanceof operator
It’s a type comparison operator which compares an object to a specified type
We can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.
Example :
We have defined a parent class (named Parent), a simple interface (named MyInterface), and a child class (named Child) that inherits from the parent and implements the interface
- interface MyInterface {
- }
interface MyInterface { }
- class Parent {
- }
class Parent { }
- class Child extends Parent implements MyInterface {
- }
class Child extends Parent implements MyInterface { }
- class InstanceofDemo {
- public static void main(String[] args) {
- Parent obj1 = new Parent();
- Parent obj2 = new Child(); // Upcasting
- System.out.println("obj1 instanceof Parent: "
- + (obj1 instanceof Parent));
- System.out.println("obj1 instanceof Child: "
- + (obj1 instanceof Child));
- System.out.println("obj1 instanceof MyInterface: "
- + (obj1 instanceof MyInterface));
- System.out.println("obj2 instanceof Parent: "
- + (obj2 instanceof Parent));
- System.out.println("obj2 instanceof Child: "
- + (obj2 instanceof Child));
- System.out.println("obj2 instanceof MyInterface: "
- + (obj2 instanceof MyInterface));
- }
- }
class InstanceofDemo { public static void main(String[] args) { Parent obj1 = new Parent(); Parent obj2 = new Child(); // Upcasting System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent)); System.out.println("obj1 instanceof Child: " + (obj1 instanceof Child)); System.out.println("obj1 instanceof MyInterface: " + (obj1 instanceof MyInterface)); System.out.println("obj2 instanceof Parent: " + (obj2 instanceof Parent)); System.out.println("obj2 instanceof Child: " + (obj2 instanceof Child)); System.out.println("obj2 instanceof MyInterface: " + (obj2 instanceof MyInterface)); } }
Note
When using the instanceof operator with null , it returns false as null is not an instance of anything.