Java Operators

What are Operators in Java?

In Java, operators are special symbols or keywords that perform specific operations on one or more operands (values or variables) to produce a result. Operators are the building blocks of Java expressions and play a crucial role in manipulating data and controlling program flow.

Types of Java Operators

Java provides a wide variety of operators categorized into the following groups:

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

Let’s explore each category in detail with examples.

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, and division.

OperatorDescriptionExample
+Additiona + b
Subtractiona – b
*Multiplicationa * b
/Divisiona / b
%Modulus (Remainder)a % b

Example Code:

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

2. Assignment Operators

Assignment operators assign values to variables.

OperatorDescriptionExample
=Assigns a valuea = b
+=Adds and assignsa += b
-=Subtracts and assignsa -= b
*=Multiplies and assignsa *= b
/=Divides and assignsa /= b
%=Calculates remainder and assignsa %= b

Example Code:

public class AssignmentExample {
public static void main(String[] args) {
int a = 5;
a += 2; // a = a + 2
System.out.println("New Value of a: " + a);
}
}

3. Relational (Comparison) Operators

Relational operators compare two values and 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 Code:

public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: " + (x == y));
System.out.println("x > y: " + (x > y));
}
}

4. Logical Operators

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

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

Example Code:

public class LogicalExample {
public static void main(String[] args) {
boolean isTrue = (5 > 2) && (3 < 5);
System.out.println("Result: " + isTrue);
}
}

5. Bitwise Operators

Bitwise operators perform operations at the binary level.

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

Example Code:

public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3; // Binary: a=101, b=011
System.out.println("Bitwise AND: " + (a & b));
}
}

6. Unary Operators

Unary operators operate on a single operand.

OperatorDescriptionExample
+Positive+a
Negative-a
++Increment++a
Decrement–a
!Logical Complement!a

Example Code:

public class UnaryExample {
public static void main(String[] args) {
int x = 5;
System.out.println("Increment: " + (++x));
System.out.println("Decrement: " + (--x));
}
}

7. Ternary Operator

The ternary operator is a shorthand for if-else statements.

Syntax:
condition ? expression1 : expression2

Example Code:

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

8. Shift Operators

Shift operators shift the bits of a number.

OperatorDescriptionExample
<<Left Shifta << 2
>>Right Shifta >> 2
>>>Unsigned Right Shifta >>> 2

Example Code:

public class ShiftExample {
public static void main(String[] args) {
int num = 8; // Binary: 1000
System.out.println("Left Shift: " + (num << 1)); // Binary: 10000
}
}

Leave a Comment