Operator In JAVA With Examples

In computer programming Operator is a symbol that tells the compiler to perform specific action which can be mathematical or logical. For example: ‘+’ is a additive operator which does addition of two number and can also be used for String concatenation.

Lets take simple example to understand operator:

int x = 4 + 5 – 7;

In the above example:

  • ‘+’ and ‘–‘ are addition and subtraction operator respectively which comes under Arithmetic Operator.)
  • “4”, “5” and “7” are operand (Operand are objects that are manipulated by operator).
  • “=” is an assignment operator which assign result of calculation (value 2 in this case) to x.

Important Note: Just want to make a short notice here after learning different types of operator in JAVA don’t miss the next topic operator precedence which is shared just below this topic. Operator learning is incomplete without knowing operator precedence.


Different Types Of Operator In JAVA:

JAVA supports 7 types of operator:

SIMPLE ASSIGNMENT OPERATOR:

“=” is assignment operator in JAVA language which assign the value of its right side to its left side value. The right side value can be of variable, constant or expression that result some value and left side can be a variable or object which has the capacity to possess that right side value . It is evaluated from right to left.

Example of assignment operator:

int x = 5;

Here value 5 is assigned to integer variable x.

ARITHMETIC OPERATOR:

There are 5 types of arithmetic operator in JAVA: addition (+), subtraction (-), division (/), multiplication (*) and remainder (%) operator.

Airthmetic Operator

+ (Addition Operator):

“+” is binary operator which adds two operands. Example of + operator:

int x = 10;
int y = 5;
int z = x + y;

Here in the above example the value of operands x and y (which is 10 and 5) added by the + operator and total value 15 is assigned to a new variable z.

– (Subtraction Operator):

“-“ is a binary operator which subtracts second operand from the first. Example of – operator:

int x = 10;
int y = 5;
int z = x – y;

In the above example value of y operands subtracts from x operands by – operator and result value is then assigned to new variable z.

/ (Division Operator):

/ is a binary operator which divide numerator by denominator. Example of division operator:

int x = 10;
int y = 5;
int z = x/y;

In the above example, value of x is divided by y by “/” operator and result value 2 is assigned to new variable z.

* (Multiplication Operator):

* is a binary operator which multiplies both the operands. Example of Multiplication operator:

int x = 10;
int y = 5;
int z = x*y;

In the above example, value of x and y operands is multiplied by “*” operator and result value 50 is assigned to z variable.

% (Remainder Or Modulos Operator):

% operator gives the remainder after an integer division of two operands. Example of remainder operator:

int x = 12;
int y = 5;
int z = x%y;

In the above example value % operator gives remainder of x and y division which is then assigned to new variable z. So the remainder value 2 is now stored in z variable.

UNARY OPERATOR IN JAVA:

Unary Operator are second type of operator in JAVA which is created to work with only one operand performing operations like incrementing or decrementing the operand value by one, inverting a boolean value or negating an expression. There are total 5 different types of unary operator in JAVA namely +(Unary plus operator), -( Unary minus operator), ++( Increment operator),–( Decrement operator) and !( Logical complement operator).

Unary Operator In Java
Now we will discuss each unary operator:

+(Unary plus operator):

This operator indicated positive value even though numbers are positive by default. Example of unary plus operator:

int x = +2;

In the above example value of x integer is positive 2.

-( Unary minus operator):

This operator negates an expression. Example of unary minus operator:

int x = -5;

In the above example the value of x is minus 5.

++( Increment operator):

This operator increment a value of operand by 1. Example of increment operator:

int x = 5;
x++;

In the above example the value of x is incremented by 1 using ++ operator.

–(Decrement Operator):

This operator decrement the value of operand by 1. Example of decrement operator:

int x = 5;
x--;

In the above example the value of x is decremented by 1 using — operator. The final value of x is 4.

!(Logical compliment Operator):

This operator inverts the value of a Boolean. Example of logical compliment operator:

bollean learningJava = true;
System.out.println(!learningJava);

In the above result output will be false because of using logical compliment operator.

EQUALITY AND RELATIONAL OPERATOR:

Equality and relational can be considered as relationship operator because there work is to compare the first operand to the second operand testing the validity of the specified relationship between them. If the result is true then the result will be 1 otherwise for false result is 0. This operator are mostly used in conditions.

There are 6 types of equality and relational operator in JAVA namely ==(Equal to), !=(not Equal to), >(greater than), < (less than), >=(Greater than or equal to) and <= (less than or equal to).

Equality And Relational Operator In Java
==(Equal to):

This operator checks the relationship of first operand is equal to second operand. Remember this operator is checking only if first operand is equal to second or not. It is not assigning value of second operand to first.

Important Note: Before we share example of equality and relational operator it is recommended to learn basics of if and else. Here we are only sharing basic and full separate guide will be shared in control flow statement topic.

If and else is the most basic and easy to understand control flow statement which is why we are using them here. The function of “if” is to first check the particular test and if it is found to be true then only it allows the code inside it to execute otherwise skips it and moves to the else statement if present or move to code outside the if block. Also remember, if “if” statement is found to be true then it skips all else statement present just after it.

Let us now create a simple equal to program:

int a = 5;
int b = 7;
if (a == b){
System.out.println("a is equal to b");
}
else{
System.out.println("a is not equal to b");
}

In the above case output will be “a is not equal to b” because a operand equality check with b comes out to be false. So statement/code inside if is not executed and code inside else statement gets executed.

!=(Not equal to):

This operator checks the relationship of first operand is not equal to second operand. Let us now create a simple not equal to program:

int a = 5;
int b = 7;
if (a != b){
System.out.println("a is not equal to b");
}
else{
System.out.println("a is equal to b");
}

In the above case output will be “a is not equal to b” because a operand equality check with b comes out to be true. So statement/code inside if gets executed.

< (Less Than):

This operator checks first operand less than second operand. Example of less than operator:

int a = 5;
int b = 7;
if (a < b)
{ 
 System.out.println("a is less than b"); 
} else
{ System.out.println("a is greater than or equal to b"); 
}

> (Greater than):

This operator checks first operand is greater than second operand. Example of Greater than operator:

int a = 5;
int b = 7;
if (a > b){
System.out.println("a is greater than b");
}
else{
System.out.println("a is less than or equal to b");
}

In the above case output will be “a is less than or equal to b” because the result of a operand greater than operator checks with b comes out to be false. So statement/code inside if is skipped and else statement gets executed.

<= (Lesser than or equal to):

This operator checks first operand is lesser than or equal to the second operand. Example of lesser than or equal to operator:

int a = 5;
int b = 5;
if (a <= b)
{
System.out.println("a is either equal or lesser than b"); 
} else{ 
System.out.println("a is greater than b"); 
}

In the above case output will be “a is either equal or lesser than b” because the result of a operand lesser than or equal to operator checks with b comes out to be true. So statement/code inside if is executed.

>= (Greater than or equal to):

This operator checks whether first operand is greater than or equal to the second operand. Example of greater than or equal to operator:

int a = 7;
int b = 5;
if (a >= b){
System.out.println("a is either equal or greater than b");
}
else{
System.out.println("a is lesser than b");
}

In the above case output will be “a is either equal or greater than b” because the result of a operand greater than or equal to operator checks with b comes out to be true. So statement/code inside if is executed.

CONDITIONAL OPERATOR:

Conditional Operator gives result based on evaluating two or more boolean expressions. There are three types of conditional operator in JAVA: && (Conditional-AND), || (Conditional-OR) and ?: (Ternary). We will discuss all one by one:

Conditional Operator In Java
&& (Conditional-AND) Operator:

Conditional AND operator takes two operands and both the operand has to be boolean expression (Expression whose output is either true or false). The && operator return true only if both the operands are true otherwise it return false. In other words, if any one or both boolean expression is false, this operator will return false.

Example of Conditional AND operator:

Lets suppose we want to create a program which only accept people of age between 18 and 28:

int xAge = 25;
if(xAge>=18 && xAge <= 28 ){
System.out.println("You are accepted");
}
else{
System.out.println("Not accepted");
}

In the above example both the boolean expression are true so the logical AND(&&) operator will return true and thus the output will be You are accepted.

|| (Conditional-OR) Operator:

This operator also accept two operands and both the operands are boolean expression. The operator returns false if both the operand are false otherwise it return true. In other words, if any one boolean expression is true out of two, this operator will return true.

Example of Conditional-OR Operator:

Lets create a simple program where two types of peoples are accepted. Either whose age is less than 18 or greater than 28.

int xAge = 17;
if(xAge < 18 || xAge > 28) {
System.out.println("You are accepted");
}else
{
System.out.println("You are not accepted");
}

In the above example, since first boolean expression is true and we know if any boolean expression is true the logical OR operator will return true. Thus, output will be You are accepted.

?: (Ternary Operator):

Ternary Operator takes three operand in which first one is boolean expression, second and third operands are values. It is called ternary because it accepts three operands.

In Ternary Operator, if the first operand which is boolean expression is true, then the value of second operand is return otherwise value of third operand is return. In other words, if boolean expression is false, the value of third operand will be return otherwise second operand will return.

Important Note: Ternary operator is a shorter sytax of if then else.

Example of ternary operator:
Lets create a program to know whether the number is even:

int x = 9;
String output = (x%2 ==0): "Even" : "Not even";
System.out.println(output);

If we divide 9 by 2 the remainder is 1 which is not equal to 0. So in the above example the first operand which is boolean expression is false. Thus the value of third operand is return and out is Not even.

TYPE COMPARISON OPERATOR:

instanceof:

instanceof is also called type comparison operator. It compares an object to a specified type.

Important Note: Before you understand it, you must know What is Object, and Class in JAVA. Below we sharing only the basic of each topic:

Object is any entity that has state & behavior and class is a group of objects that has common properties. In other words Class is a blueprint or template from which objects are created. So objects are result(instance) of Class.

instanceof operator is used to check whether the object is of particular class, subclass or interface type. It returns value true or false. If an object after comparison is find to be a specified type then it returns true otherwise false.

instanceof work on IS~A principle. IS-A is a way of saying, “this object is a type of that class.”

Syntax of instanceof Operator:

object instanceof type

It return true if object is of specified type as assigned on the right side. Example of instanceof Operator:

class Abhiandroid{
public static void main(String args[]){
Abhiandroid uidesign =new Abhiandroid();
System.out.println(uidesign instanceof Abhiandroid);
}
}

In the above example the output will be true since object uidesign is of Abhiandroid class type.

Important Note: If you found this instanceof operator confusing, we recommend you first understand the concept of class, object, OOPs concept, create few complex programs and comeback to this topic again. Then you will understand it more deeply and clearly.

BITWISE AND BIT SHIFT OPERATOR:

Bitwise Operator

Important Note: Bitwise and Bit Shift Operator are hardly used in JAVA and very rarely used in Android. So you can ignore this operator.

As from the name Bitwise sound these operator performs the operation on bit value. And also you must have heard bit is smallest unit of memory. This is represented by either 0 or 1 which means you have only one option to mark your answer. Also you can understand this concept by an electrical switch which is connected with a bulb. There can only be two conditions, one is bulb can be ON and second is it can be OFF. This is exact representation of a single bit. The combination of 4 bits is called as nibble. Bitwise common Operator are: Bitwise AND (&), Bitwise OR(|) and Bitwise exclusive OR(^):

For complete information read Bitwise Tutorial with example.

Shift: LeftShift And Rightshift Operator

Shift Operator is of two types Bitwise Left Shift And Right Shift operator. In the Bitwise shift operator, the result comes applying these operator on the bit values.

For complete information read Shift: LeftShift And RightShift Tutorial with example.


Operator Precedence:

So now you have learned all types of operator in JAVA. You now also know operator are special symbols in JAVA which perform specific operation to give specific result on operand which can be one, two or three. You now also know how to use this operand.

But before you start using operator in JAVA you must also know one more concept called Operator Precedence. This operator precedence has all operator listed according to precedence order. Operator with higher precedence are executed first relative to the lower precedence operator.

For example, suppose we are using two operator multiplication and addition, then according to operator precedence multiply will be evaluated first and then addition.

int x = 5*8+5;

Output will be 45 and not 65 because multiply operator(*) will be executed first relative to addition(+) operator.

Below is Operator Precedence Table:

Operator Precedence
Operator Precedence
postfix expr++, expr–
unary ++expr, –expr, +expr, -expr, ~, !
multiplicative *, /, %
additive +, –
shift <<, >>, >>>
relational <, >, <=, >=, instanceof
equality ==, !=
bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
logical AND &&
logical OR ||
ternary ? :
assignment =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=

Operator at the top are have higher precedence and Operator on the same line have equal precedence. When operator of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operator except for the assignment operator are evaluated from left to right; assignment operator are evaluated right to left.

Leave a Reply

Your email address will not be published. Required fields are marked *