Chapter 3: Java Operators

Operators in Java

An operator is a character that represents an action, for example + is an arithmetic operator that represents addition.

Types of Operator in Java
  1. Arithmetic Operators
  2. Assignment Operators
  3. Unary Operators
  4. Logical Operators
  5. Comparison (relational) operators
  6. Bitwise Operators
  7. Ternary Operator

 

1. Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %

  • +  is for addition.
  •   is for subtraction.
  • *  is for multiplication.
  • /  is for division.
  • %  is for modulo.

Note: Modulo operator returns remainder, for example 10 % 5 would return 0

Example of Arithmetic Operators
public class DemoArithmeticOperator {
	public static void main(String[] args) {
		
		int value1 =50;
	      int value2 =70;

	      System.out.println("value1 + value2: " + (value1 + value2) );
	      System.out.println("value1 - value2: " + (value1 - value2) );
	      System.out.println("value1 * value2: " + (value1 * value2) ); 
	      System.out.println("value1 / value2: " + (value1 / value2) );
	      System.out.println("value1 % value2: " + (value1 % value2) );
	}

}

Output:

value1 + value2: 120
value1 - value2: -20
value1 * value2: 3500
value1 / value2: 0
value1 % value2: 50

2) Assignment Operators

Assignments operators in java are: =, +=, -=, *=, /=, %=

OperatorsDescription
num2 = num1 would assign value of variable num1 to the variable.
num2+=num1 is equal to num2 = num2+num1
num2-=num1 is equal to num2 = num2-num1
num2*=num1is equal to num2 = num2*num1
num2/=num1 is equal to num2 = num2/num1
num2%=num1  is equal to num2 = num2%num1
Example of Assignment Operators
public class DemoAssignmentOperator {
	public static void main(String[] args)

	{
		int value1 = 30;
		int value2 = 60;

		value2 = value1;
		System.out.println("= Output: " + value2);

		value2 += value1;
		System.out.println("+= Output: " + value2);

		value2 -= value1;
		System.out.println("-= Output: " + value2);

		value2 *= value1;
		System.out.println("*= Output: " + value2);

		value2 /= value1;
		System.out.println("/= Output: " + value2);

		value2 %= value1;
		System.out.println("%= Output: " + value2);

	}

} 

Output:

= Output: 30
+= Output: 60
-= Output: 30
*= Output: 900
/= Output: 30
%= Output: 0

3)  Unary Operators

++ and —

OperatorsDescription
num++ is equivalent to num=num+1;
num–- is equivalent to num=num-1;
Example of Unary Operators
public class DemoUnaryOperators {
	public static void main(String[] args)

	{

		int value1 = 10;
		int value2 = 20;
		value1++;
		value2--;
		System.out.println("value1++ is: " + value1);
		System.out.println("value2-- is: " + value2);

	}

Output:

value1++ is: 11
value2-- is: 19

4) Logical Operators

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.

Logical operators in java are: &&, ||, !

Let’s say we have two boolean variables b1 and b2.

OperatorsDescription
b1&&b2will return true if both b1 and b2 are true else it would return false.
 b1||b2will return false if both b1 and b2 are false else it would return true.
 !b1would return the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true.
Example of Logical Operators
public class DemoLogicalOperator {
	public static void main(String[] args)

	{
		boolean b1 = true;
		boolean b2 = false;

		System.out.println("b1 && b2: " + (b1 && b2));
		System.out.println("b1 || b2: " + (b1 || b2));
		System.out.println("!(b1 && b2): " + !(b1 && b2));

	}
}

Output:

b1 && b2: false
b1 || b2: true
!(b1 && b2): true

5) Comparison(Relational) operators

We have six relational operators in Java: ==, !=, >, <, >=, <=

OperatorsDescription
== returns true if both the left side and right side are equal
!= returns true if left side is not equal to the right side of operator.
> returns true if left side is greater than right.
< returns true if left side is less than right side.
>= returns true if left side is greater than or equal to right side.
<= returns true if left side is less than or equal to right side.
Example of Relational operators
public class DemoRelationalOperator {
	public static void main(String[] args)

	{
		int value1 = 40;
	      int value2 = 30;
	      if (value1==value2) {
		 System.out.println("value1 and value2 are equal");
	      }
	      else{
		 System.out.println("value1 and value2 are not equal");
	      }

	      if( value1 != value2 ){
		 System.out.println("value1 and value2 are not equal");
	      }
	      else{
		 System.out.println("value1 and value2 are equal");
	      }

	      if( value1 > value2 ){
		 System.out.println("value1 is greater than value2");
	      }
	      else{
		 System.out.println("value1 is not greater than value2");
	      }

	      if( value1 >= value2 ){
		 System.out.println("value1 is greater than or equal to value2");
	      }
	      else{
		 System.out.println("value1 is less than value2");
	      }

	      if( value1 < value2 ){
		 System.out.println("value1 is less than value2");
	      }
	      else{
		 System.out.println("value1 is not less than value2");
	      }

	      if( value1 <= value2){
		 System.out.println("value1 is less than or equal to value2");
	      }
	      else{
		 System.out.println("value1 is greater than value2");
	}
}

}

Output:

value1 and value2 are not equal
value1 and value2 are not equal
value1 is greater than value2
value1 is greater than or equal to value2
value1 is not less than value2
value1 is greater than value2

6) Bitwise Operators

There are bitwise Operators: &, |, ^, value1 = 11; /* equal to 00001011*/
value2 = 22; /* equal to 00010110 */

Bitwise operator performs bit by bit processing.

OperatorDescription
&(Binary AND Operator)There are two types of AND operators in Java: the logical && and the binary &. Binary & operator work very much the same as logical && operators works, except it works with two bits instead of two expressions. The “Binary AND operator” returns 1 if both operands are equal to 1.
|(Binary OR Operator)Like “AND operators “, Java has two different “OR” operators: the logical || and the binary |. Binary | Operator work similar to logical || operators works, except it, works with two bits instead of two expressions. The “Binary OR operator” returns 1 if one of its operands evaluates as 1. if either or both operands evaluate to 1, the result is 1.
^(Binary XOR Operator)It stands for “exclusive OR” and means “one or the other”, but not both. The “Binary XOR operator” returns 1 if and only if exactly one of its operands is 1. If both operands are 1, or both are 0, then the result is 0.

Example of Bitwise Operators

public class DemoBitwiseOperator {
	public static void main(String[] args)

	{
		 int value1 = 11;  /* 11 = 00001011 */
	     int value2 = 22;  /* 22 = 00010110 */
	     int result = 0;

	     result = value1 & value2;   
	     System.out.println("value1 & value2: "+result);

	     result = value1 | value2;   
	     System.out.println("value1 | value2: "+result);
	    
	     result = value1 ^ value2;   
	     System.out.println("value1 ^ value2: "+result);
	    
	    
	}
}

Output:

value1 & value2: 2
value1 | value2: 31
value1 ^ value2: 29

7) Ternary Operator

This operator evaluates a boolean expression and assign the value based on the result.
Syntax:

variable value1 = (expression) ? value if true : value if false

If the expression results true then the first value before the colon (:) is assigned to the variable value1 else the second value is assigned to the value1.

Example of Ternary Operator
public class DemoTernaryOperator {

	public static void main(String args[]) {
		int value1, value2;
		value1 = 30;
		/*
		 * value1 is not equal to 10 that's why the second value after colon is assigned
		 * to the variable value2
		 */
		value2 = (value1 == 20) ? 100 : 200;
		System.out.println("value2: " + value2);

		/*
		 * value1 is equal to 30 that's why the first value is assigned to the variable
		 * value2
		 */
		value2 = (value1 == 30) ? 100 : 200;
		System.out.println("value2: " + value2);
	}
}

Output:

value2: 200
value2: 100

See this video to understand more about Operators.