Learn Operators In Java

Hello everyone!! Today we are going to learn operators in java.

In this post we will see what is an operator in java and its types and also how operators are helpful in java programming.

what is an operator in java?

Operator in java is nothing but a symbol to perform operations.

Learn Operators In Java: types of operator

Here are the types of operators in java,

  • Arithmetic Operator
  • Unary Operator
  • Logical Operator
  • Shift Operator
  • Relational Operator
  • Bitwise Operator
  • Ternary Operator
  • Assignment Operator

Arithmetic Operator

These are some of the important arithmetic operators in java,

// "+" operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 5;
int b = 9;
int result;
 
result = a + b;

System.out.println(result);

}

}

Output: 14

// "-" operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 10;
int b =  5;
int result;
 
result = a - b;

System.out.println(result);

}

}

Output: 5

// "/" operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 10;
int b =  5;
int result;
 
result = a / b;

System.out.println(result);

}

}

Output: 2

// "%" operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 10;
int b =  5;
int result;
 
result = a % b;

System.out.println(result);

}

}

Output: 0


unary operator

Here we learn what is post-increment and pre-increment operations effectively.

// post-increment operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 5;
 
a++;
 
System.out.println(a);

}

}

Output: 6

In the post-increment operation value of variable ‘a’ will be changed only after this plus symbol. So this plus symbol will increment the value of ‘a’ by 1.

// pre-increment operator

public class Sample_Example {

public static void main(String[] args) {
 
int a = 5;
 
++a;
 
System.out.println(a);

}

}

Output: 6

In pre-increment operation the value of variable ‘a’ is increased by 1 first.


logical operator

Logical operator is used when you want to evaluate more than one condition.

// && -> AND logical operator

public class Sample_Example {

public static void main(String[] args) {
 
int course1 = 40;
int course2 = 60;
 
if((course1 >= 35) && (course2 >= 35))
{
System.out.println("condition is true!");
}
else
{
System.out.println("condition is false!");
}
}
}

Output: condition is true!

// || -> OR logical operator

public class Sample_Example {

public static void main(String[] args) {
 
int course1 = 20;
int course2 = 25;
 
if((course1 >= 35) || (course2 >= 35))
{
System.out.println("condition is true!");
}
else
{
System.out.println("condition is false!");
}
}
}

Output: condition is false!


shift operator

There are two shift operators,

  • << left shift operator
  • >> right shift operator

Let us understand left shift operator with an example, say,

Left Shift

m = 00110011
n = 2
m << n = 11001100

Here m left shift n means,

“m” specifies the value to be shifted and “n” specifies number of positions that bits in the value are to be shifted. So each bit in the “m” will be moved to left side by 2 positions. And the output is m << n = 11001100.

We will see one sample program,

// << - Left shift operator

public class Sample_Example {

public static void main(String[] args) {
 
int x = 51;
System.out.println("Binary value of x is: " + Integer.toBinaryString(x) + "\n");
 
int y = 2;
 
int result = x << y;
 
System.out.println("Binary value after left shift(<<) operator: " + Integer.toBinaryString(result));
System.out.println("Result: " + result);
}
}

Output:

Learn Operators In Java

Now we will understand right shift operator,

Right Shift

m = 11001100
n = 2
m >> n = 00110011

Here m right shift n means,

“m” specifies the value to be shifted and “n” specifies number of positions that bits in the value are to be shifted. So each bit in the “m” will be moved to right side by 2 positions. And the output is m >> n = 00110011.

Sample program,

// >> - Right shift operator

public class Sample_Example {

public static void main(String[] args) {
 
int x = 204;
System.out.println("Binary value of x is: " + Integer.toBinaryString(x) + "\n");
 
int y = 2;
 
int result = x >> y;
 
System.out.println("Binary value after right shift(>>) operator: " + Integer.toBinaryString(result));
System.out.println("Result: " + result);
}
}

Output:

Learn Operators In Java

 


relational operator

Here i will discuss relational operators. Basically relational operators give us the ability to compare two things usually numbers. There are six relational operators,

  • equal to (=)
  • less than (<)
  • greater than (>)
  • not equal to (≠)
  • greater than or equal to (≥)
  • less than or equal to (≤)

In java these operators allow us to take two values and determine whether they fulfill the specified relationship. The output of relational operator is always boolean value that is either it will give us a true or it will give us a false.

Let’s begin with practical implementation of relational operators,

public class Sample_Example {

public static void main(String[] args) {
 
int x = 10;
int y = 20;
System.out.println("To check if value of x and y are equal: " + (x == y));
System.out.println("To check if the value of x and y are not equal: " + (x != y));
System.out.println("To check if the value of x greater than y: " + (x > y));
System.out.println("To check if the value of x is lesser than y: " + (x < y));
System.out.println("To check if the value of x is greater than or equal to y: " + (x >= y));
System.out.println("To check if the value of x is lesser than or equal to y: " + (x <= y));
}
}

Output:

Learn Operators In Java


bitwise operator

Bitwise operator is similar to your logic gate. In this we use the same truth table that we use with our logic gates. Mainly there are four types of bitwise operator,

  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)

Let’s see practical implementation of bitwise operators,

Also Read – Add Two Numbers With Bitwise Operator

public class Sample_Example {

public static void main(String[] args) {
 
int x = 1;
int y = 5;
System.out.println("Bitwise AND : " + (x & y));
System.out.println("Bitwise OR : " + (x | y));
System.out.println("Bitwise XOR : " + (x ^ y));
System.out.println("Bitwise NOT : " + (~x));
 
}
}

Output:

Learn Operators In Java


ternary operator

Ternary operator uses three operands and ternary operator is shorthand for if-then-else statement.

Now let’s understand ternary operator with an example,

public class Sample_Example {

public static void main(String[] args) {
 
int a = 10;
int b = 20;
int result;
boolean flag = true;
 
result = flag ? a : b;
System.out.println("If flag is true: " + result);
 
flag = false;
 
result = flag ? a : b;
 
System.out.println("If flag is false: " + result);
}
}

Output:

Learn Operators In Java

As you can see in the above example if boolean flag is true, we will assign the value of ‘a’ to result. Else we assign the value of ‘b’ to result.


assignment operator

Assignment operators are mainly used to assign some values to variables. It may be after doing operation to the value you assigned. Here is the list of assignment operators,

  • equal to (=)
  • add and assign (+=)
  • subtract and assign (-=)
  • multiply and assign (*=)
  • divide and assign (/=)
  • modulus and assign (%=)
  • left shift and assign (<<=)
  • right shift and assign (>>=)
  • bitwise AND assignment(&=)
  • bitwise exclusive OR and assign (^=)
  • bitwise inclusive OR and assign (|=)

Let’s see example,

public class Sample_Example {

public static void main(String[] args) {
 
int a = 10;
int b = 20;
 
int assign = 30;
 
int c = 40;
c+=a;
 
int d = 50;
d-=b;
 
int e = 100;
e*=a;
 
int f = 3000;
f/=assign;
 
int g = 300;
g%=b;
 
int h = 21;
h <<= 3;
 
int i = 17;
i >>= 2;
 
int j = 13;
j &= 13;
 
int k = 21;
k ^= 14;
 
int l = 29;
l |= 12;
 
System.out.println("Equal: " + assign);
System.out.println("Add and assign: " + c);
System.out.println("Subtract and assign: " + d);
System.out.println("Multiply and assign: " + e);
System.out.println("Divide and assign: " + f);
System.out.println("Modulus and assign: " + g);
System.out.println("Left shift and assign: " + h);
System.out.println("Right shift and assign: " + i);
System.out.println("Bitwise AND and assign: " + j);
System.out.println("Bitwise exclusive OR and assign: " + k);
System.out.println("Bitwise inclusive OR and assign: " + l);
 
}
}

Output:

Learn Operators In Java


Learn Operators In Java: Java operators and precedence

Learn Operators In Java

About the author

admin

View all posts

Leave a Reply

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