Java operators

Question

Answers ( 1 )

    0
    2024-02-02T15:04:05+00:00

    Java operators are special symbols used in Java programming language to perform operations on variables and values. These operators can be categorized into several types:

    1. Arithmetic Operators: Used for basic mathematical operations.

      • + (Addition): Adds two values.
      • - (Subtraction): Subtracts one value from another.
      • * (Multiplication): Multiplies two values.
      • / (Division): Divides one value by another.
      • % (Modulus): Returns the remainder of a division.

      Example:

      int a = 10, b = 5;
      int sum = a + b; // 15
      int difference = a - b; // 5
      int product = a * b; // 50
      int quotient = a / b; // 2
      int remainder = a % b; // 0
      
    2. Relational Operators: Used to compare two values.

      • == (Equal to): Checks if two values are equal.
      • != (Not Equal to): Checks if two values are not equal.
      • > (Greater than): Checks if the left value is greater than the right.
      • < (Less than): Checks if the left value is less than the right.
      • >= (Greater than or equal to): Checks if the left value is greater than or equal to the right.
      • <= (Less than or equal to): Checks if the left value is less than or equal to the right.

      Example:

      int x = 10, y = 20;
      boolean result1 = (x == y); // false
      boolean result2 = (x != y); // true
      boolean result3 = (x > y);  // false
      boolean result4 = (x < y);  // true
      boolean result5 = (x >= y); // false
      boolean result6 = (x <= y); // true
      
    3. Logical Operators: Used to perform "logical AND" and "logical OR" operations.

      • && (Logical AND): Returns true if both expressions are true.
      • || (Logical OR): Returns true if at least one expression is true.
      • ! (Logical NOT): Reverses the logical state of its operand.

      Example:

      boolean a = true, b = false;
      boolean result1 = a && b; // false
      boolean result2 = a || b; // true
      boolean result3 = !a;     // false
      
    4. Assignment Operators: Used to assign values to variables.

      • = (Simple assignment): Assigns right operand to the left operand.
      • +=, -=, *=, /=, %=: Perform the operation and assign the result.

      Example:

      int a = 10;
      a += 5; // a = a + 5, so a is now 15
      a -= 3; // a = a - 3, so a is now 12
      
    5. Bitwise Operators: Operate on bits and perform bit-by-bit operations.

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

      Example:

      int a = 5; // 0101 in binary
      int b = 3; // 0011 in binary
      int result = a & b; // 0101 & 0011 = 0001, which is 1 in decimal
      
    6. Ternary Operator (Conditional Operator): Short form of if-else statement.

      • ?: Evaluates a boolean expression and returns one value if true and another if false.

      Example:

      int a = 10, b = 5;
      int result = (a > b) ? a : b; // result is 10 because a is greater than b
      

    These operators are fundamental to performing various operations in Java programming and are used extensively in different types of applications.

Leave an answer