JAVA Tutorial



OPERATORS IN JAVA


Operators in Java

In Java, operators are special symbols or keywords used to perform operations on variables and values. Operators are classified into various types:

1. Arithmetic Operators

These operators perform basic mathematical operations:

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

2. Relational Operators

These operators compare two values and return a boolean result:

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

3. Logical Operators

These operators are used to combine multiple conditions:

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

4. Assignment Operators

These operators are used to assign values to variables:

  • = (Simple assignment)
  • += (Add and assign)
  • -= (Subtract and assign)
  • *= (Multiply and assign)
  • /= (Divide and assign)
  • %= (Modulus and assign)

5. Unary Operators

These operators work on a single operand:

  • + (Unary plus)
  • - (Unary minus)
  • ++ (Increment)
  • -- (Decrement)
  • ! (Logical NOT)

6. Ternary Operator

The ternary operator is a shorthand for a simple if-else statement:

condition ? expression1 : expression2;
  

🔧 Example:

If num > 10, then set result = "Greater"; otherwise, set result = "Smaller":

String result = (num > 10) ? "Greater" : "Smaller";
  

7. Bitwise Operators

These operators perform bit-level operations:

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

8. Instanceof Operator

The instanceof operator checks whether an object is an instance of a particular class or subclass:

if (object instanceof ClassName) {
    // code block
}
  

📱 Mobile-Friendly Tip

Keep conditions simple for better readability on smaller screens. Break long conditional expressions into multiple lines or use simpler operators.

🎯 Best Practices

  • Use descriptive names: For better clarity, use meaningful variable names when working with operators.
  • Avoid too many nested ternary operators: They can make your code less readable.

Quick Tip:

In most cases, try to avoid using multiple operators in one line as it can make your code harder to debug.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review