Java Operators


Java Operators

In Java, operators are special symbols or keywords used to perform specific operations on variables and values. These are essential building blocks of Java programs, enabling developers to execute computations, assign values, and make decisions. Operators in Java are categorized based on their functionality.


1. Arithmetic Operators

These operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example:

public class ArithmeticExample {
    public static void main(String[] args) {
        int a = 15, b = 7;
        System.out.println("Addition: " + (a + b));  // 22
        System.out.println("Subtraction: " + (a - b));  // 8
        System.out.println("Multiplication: " + (a * b));  // 105
        System.out.println("Division: " + (a / b));  // 2
        System.out.println("Modulus: " + (a % b));  // 1
    }
}

2. Relational Operators

Relational operators evaluate the relationship between two values and produce a boolean outcome (true or false).

OperatorDescriptionExample
==Equal toa == b
!=Not equal toa != b
>Greater thana > b
<Less thana < b
>=Greater than or equal toa >= b
<=Less than or equal toa <= b

Example:

public class RelationalExample {
    public static void main(String[] args) {
        int a = 25, b = 15;
        System.out.println(a == b);  // false
        System.out.println(a != b);  // true
        System.out.println(a > b);   // false
        System.out.println(a < b);   // true
        System.out.println(a >= b);  // false
        System.out.println(a <= b);  // true
    }
}

3. Logical Operators

OperatorDescriptionExample
&&Logical ANDa > 5 && b < 10
|| Logical OR a > 5 || b < 10
!Logical NOT!(a > 5)

Example:

public class LogicalExample {
    public static void main(String[] args) {
        int a = 7, b = 12;
        System.out.println((a > 5) && (b < 15));  // true
        System.out.println((a > 10) || (b < 15));  // true
        System.out.println(!(a > 5));  // false
    }
}

4. Bitwise Operators

OperatorDescriptionExample
&Bitwise ANDa & b
| Bitwise OR a | b
^Bitwise XORa ^ b
~Bitwise Complement~a

Example:

public class BitwiseExample {
    public static void main(String[] args) {
        int a = 5, b = 3; // Binary: 5 -> 0101, 3 -> 0011
        System.out.println(a & b);  // 1 (0001)
        System.out.println(a | b);  // 7 (0111)
        System.out.println(a ^ b);  // 6 (0110)
        System.out.println(~a);     // -6 (inverts all bits)
    }
}

5. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExample
=Assigna = b
+=Add and assigna += b
-=Subtract and assigna -= b
*=Multiply and assigna *= b
/=Divide and assigna /= b
%=Modulus and assigna %= b

Example:

public class AssignmentExample {
    public static void main(String[] args) {
        int a = 10;
        a += ;  // a = a + 6
        System.out.println(a);  // 16
  int a = 12;
        a -= 5;  // a = a - 5
        System.out.println(a);  // 7
    }
}

6. Unary Operators

These work on a single operand.

OperatorDescriptionExample
+Unary plus+a
-Unary minus-a
++Increment (pre/post)++a, a++
--Decrement (pre/post)--a, a--
!Logical NOT!a

Example:

public class UnaryExample {
    public static void main(String[] args) {
        int a = 6;
        System.out.println(++a);  // 7
        System.out.println(a--);  // 7
        System.out.println(a);    // 6
    }
}

7. Ternary Operator

The ternary operator provides a concise way to express an if-else condition.

SyntaxExample
condition ? value1 : value2a > b ? a : b
public class TernaryExample {
    public static void main(String[] args) {
        int a = 12, b = 25;
        int max = (a > b) ? a : b;
        System.out.println("Maximum: " + max);  // 25
    }
}

8. Shift Operators

These shift bits of a number to the left or right.

OperatorDescriptionExample
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example:

public class ShiftExample {
    public static void main(String[] args) {
        int a = 8; // Binary: 1000
        System.out.println(a << 2);  // 32 (Binary: 100000)
        System.out.println(a >> 2);  // 2 (Binary: 0010)
    }
}

By mastering Java operators, developers can efficiently write expressions for computations, decisions, and logic within their programs.

PreviousNext