Java Operator

java operators Operators In Java

We have different operators in java. Since Java is inherited from C programming language. So most of the operators are similar to C programming language. But don’t worry, if you don’t know anything about C programming language. We will cover each and every aspect of operators in Java.

java operators



What is Operator

In java, Operator produces new value from one or more operands. Operands are the values present on left or right side of Operator. Operators are represented from different symbols in java like ‘+’ to add something, ‘-‘ to subtract something. When used on operands, they produce new value out of it.

Types of Operators in Java

Operators are divided into different categories in Java as below :
  1. Compound Assignment Operator
  2. Relational Operator
  3. Equality Operator
  4. instanceof Operator
  5. Arithmetic Operator
  6. The Remainder Operator
  7. String Concatenation Operator
  8. Increment and Decrements Operator
  9. Conditional Operator
  10. Logical Operator
Let’s discuss one by one with some examples.

1. Compound Assignment Operator

Compound assignment operator provides us the short way to assigning result of an arithmetic operation. We have almost 11 type of compound assignment operator available but there are only 4 most commonly used compound assignment operator.

  • += assigns the result of the addition.
  • -= assigns the result of the subtraction.
  • *= assigns the result of the multiplication
  • /= assigns the result of the division.
  • %= assigns the remainder of the division.
  • &= assigns the result of the logical AND.
  • |= assigns the result of the logical OR.
  • ^= assigns the result of the logical XOR.
  • <<= assigns the result of the signed left bit shift.
  • >>= assigns the result of the signed right bit shift.
  • >>>= assigns the result of the unsigned right bit shift.
Most commonly used are (+=, -=, *=, and /=) these compound assignment operators.

Example

a = a + 2;
b = b + 3 * 2;
After compound assignment operator
a += 2;
b += 3 * 2;
Important point to remember, consider below example :
a *= 3 - 1;
Remember, here in above example, expression on the right side will always get the higher precedence than the operator precedence.

So this is similar to the below
a = a * (3 - 1);

2. Relational Operator

Relational operators are mainly used to check a particular condition. Relational operator always produces either true or false value. We have mainly six relational operators in Java.

Below four operator are used to compare values :

> greater than
>=  greater than or equal to
< less than
<=  less than or equal to

Example

 public class RelationalOperatorExample {

 public static void main(String[] args) {
 
 System.out.println((2 > 3));
 System.out.println((5 < 3));
 System.out.println((2 >= 2));
 System.out.println((3 <= 3));
 
 // YOu can also assign values returned in boolean data types
 
 boolean result = 2 > 3;
 
 System.out.println("Result is : " + result);
 
 }

}

3. Equity Operator

Equity operators are also relational operator but these operators are used to compare two similar things and return boolean values. We have two equity operators as below :

== equals (equal to)
!= not equals (not equal to)

Example

public class DataTypeExample {

 public static void main(String[] args) {
 
 boolean result1 = (2 == 3);
 boolean result2 = ('a' == 'a');
 boolean result3 = (3.0 == 3L);
 boolean result4 = (45 != 30);
 
 System.out.println("Result is : " + result1); //false
 System.out.println("Result is : " + result2); //true
 System.out.println("Result is : " + result3); //true
 System.out.println("Result is : " + result4); //true
 
 }

}

4. InstanceOf Operator

InstanceOf operator is mainly used for object reference only. This operator can be used to check whether the object reference is of particular type. Type means class or interface in Java. We will discuss about class and interface later.

instanceOf(object)

Example

public class DataTypeExample {

 public static void main(String[] args) {
 
 String s = new String("Hello");
 
 if(s instanceof String) {
 System.out.println("Yes Hello is type of String class");
 }
 
 }

}

Because “Hello” is a string of java.lang.String class type. So it will print the message on console.

5. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations in Java. We have below arithmetic operators available in Java.

+ addition
– subtraction
* multiplication
/ division

Example

int a = 3 * 5;
double b = (4.4 * 3.3);

6. Remainder Operator

Remainder operator just divides the values from left side with the value from right side and result is the remainder. You can use ‘%’ operator for this operation.

Example

double b = 30 % 10 // Remainder will be 0.0

7. String Concatenation Operator

Sometimes you need to join two strings in Java then Java have overloaded operator ‘+’ for this purpose. ‘+’ operator can also be used for string concatenation.

Example

String s1 = "Hello";
String s2 = " World";
String s3 = S1 + s2; // Will result new object Hello World
Some interested thing about ‘+’ operator when used with String and numeric value. Take a look on below example.

String a = "Hello ";
int a = 10;
int b = 20;

System.out.println(a + b + c); // Will provide result Hello 1020
System.out.println(a + (b + c)); // Will provide result Hello 30
System.out.println(b + c + a); // Will provide 30Hello
This is happening because, if you have started concatenation with String in left side and numeric values in right side. Then all the numeric values will be treated as String so the output will be “Hello 1020”, But If you separate String and numeric values with parenthesis then it will first perform arithmetic operation and will simple concatenate the result with String so the output will be “Hello 30”. But If you have numeric values in left side and String values in right side then arithmetic operation will be performed first and result will be concatenated to next String.

8. Increment and Decrements Operator

Increment and Decrements operator are used to increment or decrement a variable value by 1. There are two types of increment and decrement operators ‘++’ and ‘–‘;

Example


int a = 1;
int b = a++; // b will hold the value 1, because assignment will be performed first.
int c = a++; // c will hold the value 2, because a incremented once.
int d = ++a; // d will hold the value 4, first increment then assignment.

int e = --a; // e will hold value 3, first decrement then assignment.

9. Conditional Operator

Conditional operator is a ternary operator because it has three operands. This operator is short form of if else condition in java. Conditional operator evaluates values based on true / false condition and assign final result.

variable = (boolean Expression) ? value to be assigned if true : value to be assigned if false.

Example

String s = (2 > 1) : "Two is greater than one" : "two is not greater than one";
// Output will be Two is greater than one.

10. Logical Operators

Local operators are used to perform logical operation on variables. We generally have six types of logical operators available in java as below :

&, |, ^, !, &&, and ||

We will separate logical operators in different categories and will discuss them one by one so that there will be no confusion.

Logical Bitwise Operator

Bitwise operators compare two variables bit by bit, and return a variable whose bits have been set based on whether the two variables being compared had respective bits that were either both “on” (&), one or the other “on” (|), or exactly one “on” (^).

Example

byte b1 = 6 & 8;
byte b2 = 7 | 9;
byte b3 = 5 ^ 4;
System.out.println(b1 + " " + b2 + " " + b3); // 0 15 1

Short Circuit Logical Operator

Short circuit logical operator mainly used to evaluate more than one logical expression in java. These operators are most commonly used operators in java. We have two short circuit logical operator :

&& short-circuit AND
|| short-circuit OR

Example

if((2 > 1) && (1 == 1)) // Will return true, because both condition are true
if((3 > 2) || (4 < 2)) // will return true, because one condition is true

Logical Operator (Non short circuit)

In you want to make sure that each side of expression is valuated then these operators are useful. Normally, in case of logical short circuit operator ‘&&’ if the first operand is false then second will not be evaluated because condition will be false and in case of ‘||’ if the first operand is true then second will not be evaluated because result will be true.

But this is not the case with non short circuit operator ‘|’ and ‘&’. In case of ‘&’, if first operand is false then next operand will be executed even if it knows that the result will be false. In case of ‘|’, if first operand is true then next expression will be evaluated even if it knows that condition will be true.

Example

int z = 5;
if(++z > 5 || ++z > 6) z++; // z = 7 after this code

int z = 5;
if(++z > 5 | ++z > 6) z++; // z = 8 after this code

Leave a Reply

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