Java – Operators

Here, we will learn about operators in java. Operators are symbols, which perform a specific task on given values. For example +,-,* are Java operators for the arithmetical operations. Java has many different types of operators, we will discuss all of them here.

Types of Operators in Java

Java has many different types of Operators, let’s discuss them.

  1. Unary Operator
  2. Binary Operator
  3. Ternary Operator
  4. Bitwise Operator

Unary Operators (++, –)

The operator with a single operand is known as a unary operator. It increases (++) or decreases (‐‐) the value of a variable by one. There are two types of unary operator.

  • Prefix ( ++var ) – the operator before the variable is called prefix. It increases the value first, then assigns it to the variable or statement.
  • Postfix ( var++ ) – the operator after the variable is called postfix. First, it assigns the value to a variable or statement, then it increases or decreases the value by 1.

Example: Implementation of the unary operator.

public class Example
{
public static void main(String arg[])
{
	int n=10;	//integer variable
	System.out.println( n );	
	//print 10

	System.out.println( n++ );	
	//above code will first print 10 then store 11 because of postfix

	System.out.println( ++n );	
	//above code will first store 12 then print 12 because of prefix

	System.out.println( n-- );	
	//above code will first print 12 then store 11 because of postfix

	System.out.println( --n );	
	//above code will first store 10 then print 10 because of prefix

	System.out.println( n );	
	//print 10
}
}		 

Binary Operators

The operators, which requires two operands in an expression is known as a binary operator.

  • Arithmetic Operator (Mathematical Operator)
  • Assignment Operator
  • Relational Operator (Comparison Operator)
  • Logical Operator

Arithmetic Operator (Mathematical Operator)

The operators, which are used for the mathematical operation.

OperatorDescription
+For Addition
For Subtraction
*Multiplication
/Divison
%Modulus (Remainder of Division)

Example: Implementation of the arithmetic operation in Java.

public class Example
{
public static void main(String arg[])
{
	int a=21, b=5;
	System.out.println(a+b);	//output 26
	System.out.println(a-b);	//output 16
	System.out.println(a*b);	//output 105
	System.out.println(a/b);	//output 4
	System.out.println(a%b);	//output 1
}
}	

Assignment Operator

The assignment operators are used to assigns values from the right side to left side operand.

OperatorExample
= a=5, 5 assign to variable a
+= a=a+b can also write as a+=b
-= a=a-b can also write as a-=b
*= a=a*b can also write as a*=b
/= a=a/b can also write as a/=b
%= a=a%b can also write as a%=b

Relational Operator (Comparison Operator)

To check the relation or compare two operands relational operators are used. It returns a boolean value as a result.

OperatorDescription
< Less then
> Greater then
<= Less then or Equal
>= Greater then or equal
==Equal to
!=Not equal to

Logical Operators

To combine relational expression in Java, logical operators are used. It returns a boolean value as a result.

Operator Name Description
&& Logical ANDReturn TRUE if all expression is true, otherwise FALSE.
|| Logical ORReturn TRUE if all or any one expression is true, otherwise FALSE.
! Logical NOTReverse the state of the expression(TRUE expression to False and FALSE to TRUE).

Ternary Operator (Conditional Operator)

The ternary or conditional operator uses three operands, so it is called ternary. It has three parts condition, true and false.

Syntax:

condition ? expression-1 : expression-2
  • ? – it is an operator
  • expression-1 – executed when the condition is true.
  • expression-2 – executed, when the condition is false.

Example: Conditional operator, to check number is Even or Odd.

public class Example
{
public static void main(String arg[])
{
	int a=20;	//integer variable
	String msg="";	//string variable to store text message
	msg = (a%2 == 0 ? "Even" : "Odd"); 
	System.out.println( msg );
}
}

In the above example, if the remainder is 0 after dividing by 2, then ‘Even’ else ‘Odd’ will store in msg variable.

Bitwise Operators

The bitwise operators are used with binary numbers. When you use bitwise with decimal numbers, first it converted decimal into binary then bitwise operator works on each bit.

Operators Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Left Shift
>> Right Shift

Example: Bitwise operator example.

public class Example
{
public static void main(String arg[])
{
	int a=11, b=3;
	//binary of 10 is 00001011 and of 3 is 00000011

	System.out.println(a & b);
	//output 3, because 00001011 & 00000011 = 0000011

	System.out.println(a | b);
	//output 11, because 00001011 | 00000011 = 00001011

	System.out.println(a ^ b);
	//output 8, because 00001011 ^ 00000011 = 00001000

	System.out.println( ~a );
	//2's complement

	System.out.println(a << b);
	//output 88, because 00001011 << 3 = 01011000
	//remove 3 digit from left and add three 0 to the right

	System.out.println(a >> b);
	//output 1, because 00001011 >> 3 = 00000001
	// remove 3 digit from right and add three 0 to the left
}
}