Operators in Java

Loading

Operators in Java are symbols that perform specific operations on one, two, or three operands (variables, values, or expressions) and return a result. Java provides a rich set of operators that can be categorized into several types based on their functionality. Below is a detailed explanation of the different types of operators in Java.


Types of Operators in Java

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Additionint sum = a + b;
-Subtractionint diff = a - b;
*Multiplicationint prod = a * b;
/Divisionint quot = a / b;
%Modulus (Remainder)int rem = a % b;

2. Unary Operators

Unary operators operate on a single operand.

OperatorDescriptionExample
+Positive (unary plus)int x = +5;
-Negative (unary minus)int y = -5;
++Increment (pre/post)x++; or ++x;
--Decrement (pre/post)y--; or --y;
!Logical NOT (boolean negation)boolean flag = !true;

3. Relational Operators

Relational operators are used to compare two values and return a boolean result (true or false).

OperatorDescriptionExample
==Equal toif (a == b)
!=Not equal toif (a != b)
>Greater thanif (a > b)
<Less thanif (a < b)
>=Greater than or equal toif (a >= b)
<=Less than or equal toif (a <= b)

4. Logical Operators

Logical operators are used to combine multiple boolean expressions.

OperatorDescriptionExample
&&Logical ANDif (a > 0 && b > 0)
||Logical ORif (a > 0 || b > 0)
!Logical NOTif (!(a > 0))

5. Bitwise Operators

Bitwise operators perform operations on individual bits of integer types (byte, short, int, long).

OperatorDescriptionExample
&Bitwise ANDint result = a & b;
|Bitwise ORint result = a | b;
^Bitwise XORint result = a ^ b;
~Bitwise NOT (complement)int result = ~a;
<<Left shiftint result = a << 1;
>>Right shift (signed)int result = a >> 1;
>>>Right shift (unsigned)int result = a >>> 1;

6. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Simple assignmentint x = 5;
+=Add and assignx += 5; (x = x + 5)
-=Subtract and assignx -= 5; (x = x – 5)
*=Multiply and assignx *= 5; (x = x * 5)
/=Divide and assignx /= 5; (x = x / 5)
%=Modulus and assignx %= 5; (x = x % 5)
&=Bitwise AND and assignx &= 5; (x = x & 5)
|=Bitwise OR and assignx |= 5; (x = x
^=Bitwise XOR and assignx ^= 5; (x = x ^ 5)
<<=Left shift and assignx <<= 1; (x = x << 1)
>>=Right shift (signed) and assignx >>= 1; (x = x >> 1)
>>>=Right shift (unsigned) and assignx >>>= 1; (x = x >>> 1)

7. Ternary Operator (Conditional Operator)

The ternary operator is a shorthand for an if-else statement.

OperatorDescriptionExample
? :Ternary operatorint max = (a > b) ? a : b;

8. instanceof Operator

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

OperatorDescriptionExample
instanceofChecks object typeif (obj instanceof MyClass)

Operator Precedence and Associativity

Operator precedence determines the order in which operators are evaluated in an expression. Associativity determines the order in which operators of the same precedence are evaluated.

Operator Precedence Table (Highest to Lowest)

PrecedenceOperator TypeOperators
1Postfixexpr++, expr--
2Unary++expr, --expr, +expr, -expr, !, ~
3Multiplicative*, /, %
4Additive+, -
5Shift<<, >>, >>>
6Relational<, >, <=, >=, instanceof
7Equality==, !=
8Bitwise AND&
9Bitwise XOR^
10Bitwise OR|
11Logical AND&&
12Logical OR||
13Ternary? :
14Assignment=, +=, -=, *=, /=, etc.

Example Program

public class Main {
    public static void main(String[] args) {
        int a = 10, b = 5;

        // Arithmetic Operators
        System.out.println("Sum: " + (a + b));
        System.out.println("Difference: " + (a - b));
        System.out.println("Product: " + (a * b));
        System.out.println("Quotient: " + (a / b));
        System.out.println("Remainder: " + (a % b));

        // Unary Operators
        System.out.println("Increment: " + (++a));
        System.out.println("Decrement: " + (--b));

        // Relational Operators
        System.out.println("a > b: " + (a > b));

        // Logical Operators
        System.out.println("a > 0 && b > 0: " + (a > 0 && b > 0));

        // Bitwise Operators
        System.out.println("a & b: " + (a & b));

        // Ternary Operator
        int max = (a > b) ? a : b;
        System.out.println("Max: " + max);
    }
}

Summary

Operator TypeKey OperatorsUse Case
Arithmetic+, -, *, /, %Basic mathematical operations
Unary+, -, ++, --, !Operations on a single operand
Relational==, !=, >, <, >=, <=Comparing values
Logical&&, ||, !Combining boolean expressions
Bitwise&, |, ^, ~, <<, >>, >>>Operations on bits
Assignment=, +=, -=, *=, /=Assigning values to variables
Ternary? :Shorthand for if-else
instanceofinstanceofChecking object type

Understanding and using operators effectively is essential for writing efficient and concise Java programs.

Leave a Reply

Your email address will not be published. Required fields are marked *