Java Tutorial: Operators, Types of Operators & Expressions in Java

 

Java Tutorial: Operators, Types of Operators & Expressions in Java

  • An operator is a symbol that the compiler to perform a specific operation on operands.
  • Example :  a + b = c
  • In the above example, 'a' and 'b' are operands on which the '+' operator is applied.

Types of operators :

  1. Arithmetic Operators :
    • Arithmetic operators are used to perform mathematical operations such as addition, division, etc on expressions.
    • Arithmetic operators cannot work with Booleans.
    • % operator can work on floats and doubles.
    • Let x=7 and y=2
      OperatorDescriptionExample 
      + (Addition)Used to add two numbersx + y = 9
      - (Subtraction)Used to subtract the right-hand side value from the left-hand side valuex - y = 5
      * (Multiplication)Used to multiply two values.x * y = 14
      / (Division)Used to divide left-hand Value by right-hand value.x / y = 3
      % (Modulus)Used to print the remainder after dividing the left-hand side value from
      the right-hand side value.
      x % y = 1
      ++ (Increment)Increases the value of operand by 1.x++ = 8
      -- (Decrement)Decreases the value of operand by 1.y-- =  1
  2. Comparison Operators :
    • As the name suggests, these operators are used to compare two operands.
    • Let x=7 and y=2

      OperatorDescriptionExample 
      == (Equal to)Checks if two operands are equal. Returns a boolean value.x == y --> False
      != (Not equalChecks if two operands are not equal. Returns a boolean value.x != y --> True
      > (Greater than)Checks if the left-hand side value is greater than the right-hand side value. Returns a boolean value.x > y --> True
      < (Less than)Checks if the left-hand side value is smaller than the right-hand side value. Returns a boolean value.x < y --> False
      >=(Greater than or equal to)Checks if the left-hand side value is greater than or equal to the right-hand side value. Returns a boolean value.x >= y --> True
      <= (Less than or equal to)Checks if the left-hand side value is less than or equal to the right-hand side value. Returns a boolean value.x <= y -->False
  3. Logical Operators :
    • These operators determine the logic in an expression containing two or more values or variables.
    • Let x = 8 and y =2
      && (logical and)Returns true if both operands are true.

      x<y && x!=y --> True

      || (logical or)Returns true if any of the operand is true.x<y && x==y --> True
      ! (logical not)Returns true if the result of the expression is false and vice-versa

      !(x<y && x==y) --> False

  4. Bitwise Operators :
    • These operators perform the operations on every bit of a number.
    • Let x =2 and y=3. So 2 in binary is 100, and 3 is 011. 

      Operator DescriptionExample
      & (bitwise and)1&1 =1, 0&1=0,1&0=0,1&1=1, 0&0 =0(A & B) = (100 & 011) = 000
      | (bitwise or)1&0 =1, 0&1=1,1&1=1, 0&0=0(A | B)  = (100 | 011 ) = 111
      ^ (bitwise XOR)1&0 =1, 0&1=1,1&1=0, 0&0=0(A ^ B) = (100 ^ 011 ) = 111
      << (left shift)This operator moves the value left by the number of bits specified. 13<<2 = 52(decimal)
      >> (right shift)This operator moves the value left by the number of bits specified. 13>>2 = 3(decimal)

Precedence of operators

The operators are applied and evaluated based on precedence. For example, (+, -) has less precedence compared to (*, /). Hence * and / are evaluated first.

In case we like to change this order, we use parenthesis ().

Code as Described in the Video

package com.company;

public class CWH_Ch2_Operators {
    public static void main(String[] args) {
        // 1. Arithmetic Operators
        int a = 4;
        // int b = 6 % a; // Modulo Operator
        // 4.8%1.1 --> Returns Decimal Remainder

        // 2. Assignment Operators
        int b = 9;
        b *= 3;
        System.out.println(b);

        // 3. Comparison Operators
        // System.out.println(64<6);

        // 4. Logical Operators
        // System.out.println(64>5 && 64>98);
        System.out.println(64>5 || 64>98);

        // 5. Bitwise Operators
        System.out.println(2&3);
        //        10
        //        11
        //        ----
        //        10
    }
}

Handwritten Notes: Click To Download

Ultimate Java Cheatsheet: Click To Download

Previous Post Next Post