Java Operators

18-Sep-2024

Learn how to use Java's arithmetic, relational, logical, and bitwise operators

Java operators can be divided into several categories based on the type of operation they perform.

 The classification of Java operators is:


  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment and Decrement Operators
  • Bitwise Operators
  • Conditional (Ternary) Operator



Arithmetic operators

Arithmetic operators in Java are used to perform basic mathematical operations on numbers.

 The main arithmetic operators in Java are:


                  Name                 Operators
Addition
                      +
Subtraction
                       -
Multiplication
                       *
Division
                      /
Modulus
                      %



public class ArithmeticOperatorsExample {
    public static void main(String[] args) {
        // Addition
        int sum = 5 + 3;
        System.out.println("Sum: " + sum);  // Output: Sum: 8

        // Subtraction
        int difference = 7 - 4;
        System.out.println("Difference: " + difference);  // Output: Difference: 3

        // Multiplication
        int product = 6 * 2;
        System.out.println("Product: " + product);  // Output: Product: 12

        // Division
        double quotient = 10.0 / 3;
        System.out.println("Quotient: " + quotient);  // Output: Quotient: 3.3333333333333335

        // Modulus (Remainder)
        int remainder = 10 % 3;
        System.out.println("Remainder: " + remainder);  // Output: Remainder: 1




Comparison operators in Java are used to compare two values ​​and determine the relationship between them.

The Java comparison operators are:

Relational Operators



                  Name                 Operators
Equal to
                      ==
Not equal to
                       !=
Less than
                       <
Greater than
                      >
Less than or equal to
                      <=
Greater than or equal to
                      >=


public class RelationalOperatorsExample {
    public static void main(String[] args) {
        int a = 5, b = 7;
        System.out.println("a == b: " + (a == b));  // false
        System.out.println("a != b: " + (a != b));  // true
        System.out.println("a > b: " + (a > b));    // false
        System.out.println("a < b: " + (a < b));    // true
        System.out.println("a >= b: " + (a >= b));  // false
        System.out.println("a <= b: " + (a <= b));  // true
    }
}




Logical Operators

Logical operators in Java are used to perform logical operations on boolean values.

Java has three main logical operators: AND (&&), OR (||), and NOT (!
).

A summary of each is as follows:

              Name                 Operators
Logical AND
                      &&
Logical OR
                       ||
Logical NOT
                       !


        public class LogicalOperatorsExample {
         public static void main(String[] args) {
             boolean isSunny = true;
             boolean isWarm = false;
     
             // Logical AND
             boolean goForWalk = isSunny && isWarm;
             System.out.println("Go for a walk: " + goForWalk);  // false
     
             // Logical OR
             boolean goForPicnic = isSunny || isWarm;
             System.out.println("Go for a picnic: " + goForPicnic);  // true
     
             // Logical NOT
             boolean stayInside = !goForPicnic;
             System.out.println("Stay inside: " + stayInside);  // false
         }
     }
       




Assignment Operators

Assignment operators in Java are used to assign values ​​to variables.

Additionally, some of these operators can perform operations in conjunction with  assignment.

Common assignment operators in Java are:

              Name                 Operators
Assign
                     =
Add and assign
                    +=
Subtract and assign
                    -=
Multiply and assign
                    *=
Divide and assign
                    /=
Modulus and assign
                   %=


               public class AssignmentOperatorsExample {
         public static void main(String[] args) {
             int x = 5;
     
             // Addition Assignment
             x += 3;  // x is now 8
     
             // Subtraction Assignment
             x -= 2;  // x is now 6
     
             // Multiplication Assignment
             x *= 4;  // x is now 24
     
             // Division Assignment
             x /= 3;  // x is now 8
     
             // Modulus Assignment
             x %= 5;  // x is now 3
     
             System.out.println("Final value of x: " + x);  // Output: Final value of x: 3
         }
     }
     



Increment and Decrement Operators

Java's increment and decrement operators  are unary operators  used to increase or decrease the value of a variable by 1, respectively.

 These operators are often used in loops and other situations where variables need to be updated by a fixed amount.

The increment and decrement operators are:

              Name                 Operators
Increment
                      ++
Decrement
                      --

        public class IncrementDecrementOperatorsExample {
         public static void main(String[] args) {
             int counter = 5;
     
             // Postfix Increment
             int result1 = counter++;
             System.out.println("Postfix Increment - Result: " + result1 + ", Counter: " + counter);
     
             // Prefix Increment
             int result2 = ++counter;
             System.out.println("Prefix Increment - Result: " + result2 + ", Counter: " + counter);
     
             // Postfix Decrement
             int result3 = counter--;
             System.out.println("Postfix Decrement - Result: " + result3 + ", Counter: " + counter);
     
             // Prefix Decrement
             int result4 = --counter;
             System.out.println("Prefix Decrement - Result: " + result4 + ", Counter: " + counter);
         }
     }
       

Bitwise operators in Java operate on individual bits of an integer value.

These operators perform bit-level operations and are often used in low-level programming, such as manipulating hardware or implementing certain algorithms.

Bitwise operators in Java are:

Bitwise Operators


         Name                 Operators
Bitwise AND
                      &
Bitwise OR
                      |
Bitwise XOR
                     ^
Bitwise NOT
                     ~
Left shift
                     <<
Right shift
                     >>
Unsigned right shift
                   >>>

         public class BitwiseOperatorsExample {
         public static void main(String[] args) {
             int a = 5;
             int b = 3;
     
             // Bitwise AND
             int resultAnd = a & b;
             System.out.println("Bitwise AND: " + resultAnd);  // Output: 1
     
             // Bitwise OR
             int resultOr = a | b;
             System.out.println("Bitwise OR: " + resultOr);    // Output: 7
     
             // Bitwise XOR
             int resultXor = a ^ b;
             System.out.println("Bitwise XOR: " + resultXor);  // Output: 6
     
             // Bitwise NOT
             int num = 5;
             int resultNot = ~num;
             System.out.println("Bitwise NOT: " + resultNot);  // Output: -6
     
             // Left Shift
             int value = 5;
             int resultLeftShift = value << 2;
             System.out.println("Left Shift: " + resultLeftShift);  // Output: 20
     
             // Right Shift
             int number = -16;
             int resultRightShift = number >> 2;
             System.out.println("Right Shift: " + resultRightShift);  // Output: -4
     
             // Unsigned Right Shift
             int unsignedNumber = -16;
             int resultUnsignedRightShift = unsignedNumber >>> 2;
             
             System.out.println("Unsigned Right Shift: " + result7);  // Output: 1
    }
}

       



Conditional (Ternary) Operator:

Conditional operators, also known as  ternary operators, are a shorthand way to express if-else statements on one line.

It takes three operands and has the following syntax:  

      Name                 Operators
Conditional operator
                      ? :




 

public class ConditionalOperatorsExample {
         public static void main(String[] args) {
             int x = 10;
     

             
     String result = (number % 2 == 0) ? "Even" : "Odd";
     
             System.out.println(result);  // Output: Even
         }
     }
     
      






Comments