Java Operators


In Java, operators are special symbols that perform operations on variables and values. Operators can be classified into several categories based on their functionality. Understanding these operators is crucial for performing calculations, comparisons, and manipulations of data in Java programming.

Types of Operators in Java

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators
  6. Unary Operators
  7. Ternary Operator (Conditional Operator)
  8. Instanceof Operator

1. Arithmetic Operators

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

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

Example:

public class ArithmeticOperatorsExample { public static void main(String[] args) { int a = 10, b = 5; System.out.println("Addition: " + (a + b)); // 15 System.out.println("Subtraction: " + (a - b)); // 5 System.out.println("Multiplication: " + (a * b)); // 50 System.out.println("Division: " + (a / b)); // 2 System.out.println("Modulus: " + (a % b)); // 0 } }

2. Relational Operators

Relational operators are used to compare two values. They return a boolean result (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 RelationalOperatorsExample { public static void main(String[] args) { int a = 10, b = 5; System.out.println("Is a equal to b? " + (a == b)); // false System.out.println("Is a not equal to b? " + (a != b)); // true System.out.println("Is a greater than b? " + (a > b)); // true } }

3. Logical Operators

Logical operators are used to perform logical operations, often with boolean values.

OperatorDescriptionExample
&&Logical AND(a > b) && (a < c)
``
!Logical NOT!(a > b)

Example:

public class LogicalOperatorsExample { public static void main(String[] args) { boolean a = true, b = false; System.out.println("a AND b: " + (a && b)); // false System.out.println("a OR b: " + (a || b)); // true System.out.println("NOT a: " + !a); // false } }

4. Bitwise Operators

Bitwise operators perform operations on bits and are used for bit-level manipulation.

OperatorDescriptionExample
&Bitwise ANDa & b
``Bitwise OR
^Bitwise XORa ^ b
~Bitwise NOT~a
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2

Example:

public class BitwiseOperatorsExample { public static void main(String[] args) { int a = 5, b = 3; // In binary: a = 0101, b = 0011 System.out.println("Bitwise AND: " + (a & b)); // 1 (0001) System.out.println("Bitwise OR: " + (a | b)); // 7 (0111) System.out.println("Bitwise XOR: " + (a ^ b)); // 6 (0110) System.out.println("Bitwise NOT: " + (~a)); // -6 } }

5. Assignment Operators

Assignment operators are used to assign values to variables. The most common assignment operator is =. There are also compound assignment operators that combine assignment with another operation.

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

Example:

public class AssignmentOperatorsExample { public static void main(String[] args) { int a = 10; a += 5; // equivalent to a = a + 5 System.out.println("Value of a: " + a); // 15 } }

6. Unary Operators

Unary operators operate on a single operand and can be used to perform various operations.

OperatorDescriptionExample
+Unary plus+a
-Unary minus (negation)-a
++Increment++a or a++
--Decrement--a or a--

Example:

public class UnaryOperatorsExample { public static void main(String[] args) { int a = 5; System.out.println("Original a: " + a); // 5 System.out.println("Incremented a: " + ++a); // 6 System.out.println("Decremented a: " + --a); // 5 } }

7. Ternary Operator (Conditional Operator)

The ternary operator is a shorthand way of expressing conditional statements. It takes three operands.

Syntax:

condition ? expression1 : expression2

If the condition is true, expression1 is evaluated; otherwise, expression2 is evaluated.

Example:

public class TernaryOperatorExample { public static void main(String[] args) { int a = 10, b = 20; int max = (a > b) ? a : b; // Assigns the maximum of a and b System.out.println("Maximum: " + max); // 20 } }

8. Instanceof Operator

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

Example:

public class InstanceofOperatorExample { public static void main(String[] args) { String name = "Alice"; boolean result = name instanceof String; // true System.out.println("Is name a String? " + result); } }

Conclusion

Java provides a rich set of operators that enable various operations on variables and values. Understanding how to use these operators effectively is crucial for writing efficient and clear Java code. Each type of operator serves a specific purpose, and knowing when and how to use them can greatly enhance your programming capabilities.