Java

Operators In Java

An operator is a symbol that tells the compiler to do something in particular. For instance, the “+” and “>” operators tell the compiler to execute addition and comparison, respectively, whereas the “=” operator tells it to conduct assignment. With the use of examples, we shall cover operations in Java in this guide.

the operator and the operand
There is always an operator and operands in any operation. Using the “+” sign as an example, the operands in a+b are a and b.

Types of Operator in Java

Java operators fall into the eight categories listed below:

1) Arithmetic Operators
2) Assignment Operators
3) Unary Operators
4) Logical Operators
5) Relational operators
6) Bitwise Operators
7) Ternary Operator
8) Shift Operators

1) Arithmetic Operators

The basic mathematical operators are +, -, *, /, and %.

Note: The modulo operator (%) returns the remainder, whereas the division (/) operator returns the quotient. For instance, 10% of 5 would result in 0 and 10% of 5 in 2.

Example of Arithmetic Operators

public class JavaExample {
  public static void main(String args[]) {
    int num1 = 100;
    int num2 = 20;

    System.out.println("num1+num2: " + (num1 + num2) );
    System.out.println("num1-num2: " + (num1 - num2) );
    System.out.println("num1*num2: " + (num1 * num2) );
    System.out.println("num1/num2: " + (num1 / num2) );
    System.out.println("num1%num2: " + (num1 % num2) );
  }
}

Output:

Operators in Java

2) Assignment Operators

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

Example of Assignment Operators

public class JavaExample {
  public static void main(String args[]) {
    int num1 = 10, num2;

    num2 = num1;
    System.out.println("= Output: "+num2);
    num2 += num1;
    System.out.println("+= Output: "+num2);
    num2 -= num1;
    System.out.println("-= Output: "+num2);
    num2 *= num1;
    System.out.println("*= Output: "+num2);
    num2 /= num1;
    System.out.println("/= Output: "+num2);
    num2 %= num1;
    System.out.println("%= Output: "+num2);
  }
}
Output:
Operators in Java

3) Unary Operators

The Java unary operators only have a single operand, as their name indicates. The following unary operators are supported by Java:

Unary less (-)
Increment(++)
Decrement(- -) (- -)
NOT(!)
Bitwise Compounding ()

num++ is equivalent to num=num+1;
num–- is equivalent to num=num-1;

Example of Unary Operators

public class JavaExample {
  public static void main(String args[]){
    int num1=100;
    int num2=200;

    //minus(-) unary operator
    int inverseNum = -num1;
    System.out.println("Opposite of num1: "+inverseNum);

    //increment
    num1++;

    //decrement
    num2--;

    System.out.println("num1++ is: "+num1);
    System.out.println("num2-- is: "+num2);

  }

Output:

Opposite of num1: -100
num1++ is: 101
num2-- is: 199
The NOT(!) Operator flips an operand’s logical state (true or false). The logical NOT operator will make a condition or operand untrue if it is true and vice versa.
If value of a boolean variable 'bool' is true, then the value of !bool is false
If the value of 'bool' is false, then the value of !bool is true

4) Logical Operators

The outcome of conditions is evaluated using logical operators. Three logical operators are available: AND (&&), OR (||), and NOT (!). When several criteria are combined and we need to assess the result as a whole, we utilize the AND and OR operators.

Example of Logical Operators

In this illustration, logical operations are being applied to boolean variables. However, these operators are utilized in real-world circumstances to combine numerous conditions (or expressions), which we discussed in a different session (link is at the end of the following program).

public class LogicalOperatorDemo {
  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

To compare two operands, utilize relational operators. The following relational operators are available in Java:

Example of Relational operators

Note: This example uses an if-else statement, which will be covered in our upcoming lesson. If you have trouble understanding this example, please see If-Else in Java.

public class JavaExample {
  public static void main(String args[]) {
    int num1 = 10;
    int num2 = 50;
    if( num1 != num2 ){
      System.out.println("num1 and num2 are not equal");
    }
    else{
      System.out.println("num1 and num2 are equal");
    }

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

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

Output:

Operators in Java

6) Bitwise Operators

Bit-level operations are carried out using bitwise operators. If two numbers (a and b) are to be combined using the AND operation, the binary representation of the two numbers must first be made. The compiler then delivers the resulting binary number’s decimal equivalent.

num1 = 11; /* equal to 00001011*/
num2 = 22; /* equal to 00010110 */

Bit by bit processing is done via the bitwise operator.

If the matching bits of num1 and num2 are equivalent, num1 & num2 compares them and creates 1, else it returns 0. Because only the second last bit of num1 and num2’s binary representations match, it would yield 2 in our example, which is 00000010.

When the matching bits of num1 and num2 are compared, num1 | num2 creates 1 if each bit is 1, else it yields 0. In this instance, it would yield 31, or 00011111.

When num1 and num2’s corresponding bits are compared, the formula yields 1 if the two are not equivalent and 0 otherwise. It would yield 29 in our case, which is identical to 00011101

The complement operator num1 simply flips the bit from one to zero and from one to one. In our case, it would yield -12, which is equivalent to 11110100 in signed 8-bit format.

In the left shift operation, num1 2, the bits are shifted to the left, the far-left bit is discarded, and the rightmost bit is given the value 0. In this instance, the output is 44, which is equal to 00101100.

num1 >> The right shift operator 2, which shifts the bits to the right, drops the far-right bit, and sets the leftmost bit’s value to 0, shifts the bits to the right. In this instance, the output is 2, which is equal to 00001010

Example of Bitwise Operators

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

     int num1 = 11;  /* 11 = 00001011 */
     int num2 = 22;  /* 22 = 00010110 */
     int result = 0;

     result = num1 & num2;   
     System.out.println("num1 & num2: "+result);

     result = num1 | num2;   
     System.out.println("num1 | num2: "+result);
    
     result = num1 ^ num2;   
     System.out.println("num1 ^ num2: "+result);
    
     result = ~num1;   
     System.out.println("~num1: "+result);
    
     result = num1 << 2;   
     System.out.println("num1 << 2: "+result); result = num1 >> 2;   
     System.out.println("num1 >> 2: "+result);
  }
}

Output:

num1 & num2: 2
num1 | num2: 31
num1 ^ num2: 29
~num1: -12
num1 << 2: 44 num1 >> 2: 2

7) Ternary Operator

The only operator in Java that accepts three operands is the ternary operator. This operator is typically used to replace an if…else statement with a single line of code.

Syntax:

variable = Condition ? Expression1: Expression2

Expression 1 runs if the condition is met.

Expression2 runs if condition is false.

If the condition is met, the result of Expression1 is assigned to the variable; otherwise, the result of Expression2 is allocated to the variable.

Example of Ternary Operator

public class TernaryOperatorDemo {

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

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

Output:

num2: 200
num2: 100

8) Shift Operators

Bit manipulation is done with shift operators. With the use of examples, we will go over the different shift operators that Java supports in this article. The following shift operators are supported by Java:

Shift operator example

public class JavaExample {
  public static void main(String[] args) {
    int num = 24;

    //Left shift
    System.out.println("num<<2: "+(num<<2));

    //Right shift
    System.out.println("num>>2: "+(num>>2));
  }
}

Output:

num<<2: 96 num>>2: 6

Operator Precedence in Java

When an expression includes more than one operator, this indicates which operator has to be evaluated first. higher precedence operators at the top and lower precedence operators at the bottom.

Operators Precedence
Unary Operators ++  – –  !  ~
Multiplicative *  / %
Additive +  –
Shift <<  >>  >>>
Relational >  >=  <  <=
Equality ==  !=
Bitwise AND &
Bitwise XOR ^
Bitwise OR |
Logical AND &&
Logical OR ||
Ternary ?:
Assignment =  +=  -=  *=  /=  %=  >  >=  <  <=  &=  ^=  |=

Conclusion

With the help of examples, we learnt several types of Java operators in this course. Visit our section on Java programs if you wish to practice the programs. Refer to java tutorial for beginners to discover more about such in-depth guides and tutorials.

Related Articles

Leave a Reply

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

Back to top button