Operators in Java

Operators in Java

  • Operators in java are special symbols or keywords utilized to perform operations on variables or values.
  • Java operators are classified into a few categories, depending on the type of operation they perform.
  • They permit developers to control information, perform calculations, compare values, and manage control flow.
  • Understanding the diverse types of operators in Java is fundamental for composing viable and productive code
Java Operators
Java Operators

Types of Operators in Java

Java operators are broadly categorized into the following groups:

1. Arithmetic Operators

Arithmetic operators are utilized to perform fundamental mathematical operations on numeric values (integers, floats, etc.).

  • + (Addition): Adds two operands.
    int sum = 10 + 5; // sum = 15
  • - (Subtraction): Subtracts the second operand from the first.
    int difference = 10 - 5; // difference = 5
  • * (Multiplication): Multiplies two operands.
    int product = 10 * 5; // product = 50
  • / (Division): Divides the numerator by the denominator.
    int quotient = 10 / 5; // quotient = 2
  • % (Modulus): Returns the remainder when the first operand is divided by the second.
    int remainder = 10 % 3; // remainder = 1

2. Relational Operators

Relational operators are utilized to compare two values. They return a boolean value (true or false), demonstrating whether the comparison is true or false.

  • == (Equal to): Checks if two operands are equal.
    boolean isEqual = (10 == 5); // isEqual = false
  • != (Not equal to): Checks if two operands are not equal.
    boolean isNotEqual = (10 != 5); // isNotEqual = true
  • > (Greater than): Checks if the left operand is greater than the right.
    boolean isGreater = (10 > 5); // isGreater = true
  • < (Less than): Checks if the left operand is less than the right.
    boolean isLess = (10 < 5); // isLess = false
  • >= (Greater than or equal to): Checks if the left operand is greater than or equal to the right.
    boolean isGreaterOrEqual = (10 >= 5); // isGreaterOrEqual = true
  • <= (Less than or equal to): Checks if the left operand is less than or equal to the right.
    boolean isLessOrEqual = (10 <= 5); // isLessOrEqual = false

3. Logical Operators

Logical operators are utilized to combine different boolean expressions or conditions. They are regularly utilized in conditional statements like if and while.

  • && (Logical AND): Returns true if both operands are true.
    boolean result = (10 > 5) && (5 < 10); // result = true
  • || (Logical OR): Returns true if at least one of the operands is true.
    boolean result = (10 > 5) || (5 > 10); // result = true
  • ! (Logical NOT): Inverts the boolean value.
    boolean result = !(10 > 5); // result = false

4. Assignment Operators

Assignment operators are utilized to assign values to variables. The foremost common assignment operator is =, but there are too shorthand assignment operators for arithmetic operations.

  • = (Simple assignment): Assigns the value of the right operand to the left operand.
    int x = 5; // x = 5
  • += (Addition assignment): Adds the right operand to the left operand and assigns the result to the left operand.
    int x = 5;
    x += 3; // x = 8
  • -= (Subtraction assignment): Subtracts the right operand from the left operand and assigns the result to the left operand.
    int x = 5;
    x -= 3; // x = 2
  • *= (Multiplication assignment): Multiplies the left operand by the right operand and assigns the result to the left operand.
    int x = 5;
    x *= 3; // x = 15
  • /= (Division assignment): Divides the left operand by the right operand and assigns the result to the left operand.
    int x = 10;
    x /= 2; // x = 5
  • %= (Modulus assignment): Assigns the remainder when the left operand is divided by the right operand.
    int x = 10;
    x %= 3; // x = 1

5. Increment and Decrement Operators

These operators are used to increase or decrease the value of a variable by 1.

  • ++ (Increment): Increases the value of the operand by 1.
    • Post-increment: x++ (First returns the value, then increments.)
    • Pre-increment: ++x (Increments first, then returns the value.)
    int x = 5;
    x++; // x = 6 (post-increment)
    ++x; // x = 7 (pre-increment)
  • -- (Decrement): Decreases the value of the operand by 1.
    • Post-decrement: x-- (First returns the value, then decrements.)
    • Pre-decrement: --x (Decrements first, then returns the value.)
    int x = 5;
    x--; // x = 4 (post-decrement)
    --x; // x = 3 (pre-decrement)

6. Bitwise Operators

Bitwise operators perform operations on the binary representation of integers.

  • & (Bitwise AND): Performs a binary AND on each bit of two operands.
    int result = 5 & 3; // result = 1
  • | (Bitwise OR): Performs a binary OR on each bit of two operands.
    int result = 5 | 3; // result = 7
  • ^ (Bitwise XOR): Performs a binary XOR (exclusive OR) on each bit.
    int result = 5 ^ 3; // result = 6
  • ~ (Bitwise NOT): Inverts the bits of the operand.
    int result = ~5; // result = -6
  • << (Left shift): Shifts the bits of the operand to the left by a specified number of positions.
    int result = 5 << 1; // result = 10
  • >> (Right shift): Shifts the bits of the operand to the right by a specified number of positions.
    int result = 5 >> 1; // result = 2
  • >>> (Unsigned right shift): Shifts the bits of the operand to the right, filling with zeros.
    int result = 5 >>> 1; // result = 2

7. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It is used to assign a value based on a condition.

  • condition ? value_if_true : value_if_false
    int x = (5 > 3) ? 10 : 20; // x = 10

8. Instanceof Operator

The instanceof operator is used to check whether an object is an instance of a particular class or subclass.

  • object instanceof ClassName
    String str = "Hello";
    boolean isString = str instanceof String; // isString = true

Operators in Java are fundamental to composing functional programs. They permit for essential operations like arithmetic and comparisons, but they moreover empower more progressed tasks such as bit control and object checking. Understanding how and when to utilize these operators is fundamental for proficient coding and problem-solving in Java.

Click Here for more details

Leave a Comment