java

Java Operators


What is Operator?

Operators are symbols that perform operations on variables and values.

 

There are different types of operators, each designed to perform specific tasks.

1. Arithmetic Operators: 

These operators perform basic arithmetic operations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (remainder) (%).

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus or remainder)
     

Example

 

public class ArithmeticExample {

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

        // Addition
        int sum = num1 + num2;
        System.out.println("Sum: " + sum);

        // Subtraction
        int difference = num1 - num2;
        System.out.println("Difference: " + difference);

        // Multiplication
        int product = num1 * num2;
        System.out.println("Product: " + product);

        // Division
        int quotient = num1 / num2;
        System.out.println("Quotient: " + quotient);

        // Modulus (remainder)
        int remainder = num1 % num2;
        System.out.println("Remainder: " + remainder);
    }
}

 

2. Relational Operators: These operators are used to compare two values.

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)

 

Example

 

public class RelationalExample {

    public static void main(String[] args) {

        // Declare and initialize two variables
        int a = 10;
        int b = 20;

        // Equal to
        boolean isEqual = (a == b);
        System.out.println("Is a equal to b? " + isEqual);

        // Not equal to
        boolean isNotEqual = (a != b);
        System.out.println("Is a not equal to b? " + isNotEqual);

        // Greater than
        boolean isGreaterThan = (a > b);
        System.out.println("Is a greater than b? " + isGreaterThan);

        // Less than
        boolean isLessThan = (a < b);
        System.out.println("Is a less than b? " + isLessThan);

        // Greater than or equal to
        boolean isGreaterOrEqual = (a >= b);
        System.out.println("Is a greater than or equal to b? " + isGreaterOrEqual);

        // Less than or equal to
        boolean isLessOrEqual = (a <= b);
        System.out.println("Is a less than or equal to b? " + isLessOrEqual);
    }
}

 

3. Logical Operators: These operators perform logical operations on boolean values. 

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)
     

Example

 

public class LogicalExample {

    public static void main(String[] args) {
       
        // Declare and initialize two boolean variables
        boolean a = true;
        boolean b = false;

        // Logical AND
        boolean resultAND = a && b;
        System.out.println("Result of condition1 AND condition2: " + resultAND);

        // Logical OR
        boolean resultOR = a || b;
        System.out.println("Result of condition1 OR condition2: " + resultOR);

        // Logical NOT
        boolean resultNOT = !a;
        System.out.println("Result of NOT condition1: " + resultNOT);
    }
}

 

4. Bitwise Operators: These operators perform operations at the bit level.

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise NOT)
  • << (Left shift)
  • >> (Right shift)
  • >>> (Unsigned right shift)

 

Example
 

public class BitwiseExample {

    public static void main(String[] args) {

        // Declare and initialize two integers
        int a = 5;  // binary: 0101
        int b = 3;  // binary: 0011

        // Bitwise AND
        int resultAND = a & b;
        System.out.println("Result of bitwise AND: " + resultAND);

        // Bitwise OR
        int resultOR = a | b;
        System.out.println("Result of bitwise OR: " + resultOR);

        // Bitwise XOR
        int resultXOR = a ^ b;
        System.out.println("Result of bitwise XOR: " + resultXOR);

        // Bitwise NOT
        int resultNOTA = ~a;
        System.out.println("Result of bitwise NOT for 'a': " + resultNOTA);

        // Left shift
        int resultLeftShift = a << 1;
        System.out.println("Result of left shift for 'a': " + resultLeftShift);

        // Right shift
        int resultRightShift = a >> 1;
        System.out.println("Result of right shift for 'a': " + resultRightShift);

        // Unsigned right shift
        int resultUnsignedRightShift = a >>> 1;
        System.out.println("Result of unsigned right shift for 'a': " + resultUnsignedRightShift);
    }
}

 

5. Assignment Operators: These operators are used to assign values to variables.

  • = (Assignment)
  • += (Addition assignment)
  • -= (Subtraction assignment)
  • *= (Multiplication assignment)
  • /= (Division assignment)
  • %= (Modulus assignment)

 

Example

 

public class AssignmentExample {

    public static void main(String[] args) {

        // Declare and initialize variables
        int x = 10;
        int y = 5;

        // Assignment
        int resultAssignment = x;
        System.out.println("Result of assignment: " + resultAssignment);

        // Addition Assignment
        x += y;  // Equivalent to x = x + y;
        System.out.println("Result of addition assignment: " + x);

        // Subtraction Assignment
        x -= y;  // Equivalent to x = x - y;
        System.out.println("Result of subtraction assignment: " + x);

        // Multiplication Assignment
        x *= y;  // Equivalent to x = x * y;
        System.out.println("Result of multiplication assignment: " + x);

        // Division Assignment
        x /= y;  // Equivalent to x = x / y;
        System.out.println("Result of division assignment: " + x);

        // Modulus Assignment
        x %= y;  // Equivalent to x = x % y;
        System.out.println("Result of modulus assignment: " + x);
    }
}

 

6. Ternary Operators:

Ternary operator (also known as the conditional operator) is a shorthand way to write an if-else statement in a single line.

 

Syntax

 

result = (condition) ? expression_if_true : expression_if_false;

 

Example

 

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

        // Using the ternary operator to find the maximum number
        int max = (num1 > num2) ? num1 : num2;

        System.out.println("The maximum number is: " + max);
    }
}

 

7. Increment/Decrement Operators:

Increment and Decrement operators are unary operators used to increase or decrease the value of a variable by one.

 

Note: 

1. The increment operator '++' adds 1 to the current value of a variable. It can be used in two ways.

  • Pre-increment: The value is incremented first, and then it's used in the expression.

Example

public class IncrementDecrementExample {
    public static void main(String[] args) {
    	int x = 10;
    	int newResult = ++x; // newResult = 11, x = 11 (x is incremented before its value is used)
    	System.out.println("x = " + x);
    }
}
  • Post-increment: The value is first used in the expression, and then it's incremented.

Example

public class IncrementDecrementExample {
    public static void main(String[] args) {
    	int x = 10;
    	int Result = x++; // Result = 10, x = 11 (x is incremented after its value is used)
    	System.out.println("Result  = " + Result);
    }
}

 

2. The decrement operator '--' subtracts 1 from the current value of a variable and follows the same syntax as the increment operator.

  • Pre-decrement: The value is first used in the expression, and then it's decremented.

Example

public class IncrementDecrementExample {
    public static void main(String[] args) {
    	int x = 15;
		int newResultB = --x; // newResultB = 14, X = 14 (x is decremented before its value is used)

		System.out.println("x = " + x);
    }
}
  • Post-decrement: The value is first used in the expression, and then it's decremented.

Example

public class IncrementDecrementExample {
    public static void main(String[] args) {
    	int a = 8;
		int resultA = a--; // resultA = 8, a = 7 (a is decremented after its value is used)
		System.out.println("x = " + x);
    }
}