Operators

Operators in Java are special symbols or keywords used to perform operations on variables and values. They are essential for manipulating data and building logical expressions in programs. Java supports a variety of operators, classified into different categories based on the type of operations they perform.

 

Types of Operators

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

 

Arithmetic Operators

Arithmetic operators are used for performing mathematical operations like addition, subtraction, multiplication, etc.

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

Example:

int a = 10;
int b = 5;
System.out.println(a + b);  // Output: 15
System.out.println(a % b);  // Output: 0

 

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorDescriptionExample
=Assigns value to a variablea = 10
+=Adds and assignsa += 5 (same as a = a + 5)
-=Subtracts and assignsa -= 5 (same as a = a - 5)
*=Multiplies and assignsa *= 5
/=Divides and assignsa /= 5
%=Modulus and assignsa %= 5

Example:

int a = 10;
a += 5;  // Same as a = a + 5
System.out.println(a);  // Output: 15

 

Relational (Comparison) Operators

Relational operators are used to compare two values. The result of a comparison is a boolean value (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:

int a = 10;
int b = 5;
System.out.println(a > b);  // Output: true
System.out.println(a == b); // Output: false

 

Logical Operators

Logical operators are used to perform logical operations on boolean expressions.

OperatorDescriptionExample
&&Logical AND (true if both are true)a && b
||Logical OR (true if at least one of the operands is true)a || b
!Logical NOT (inverts the boolean value)!a

Example:

boolean a = true;
boolean b = false;
System.out.println(a && b);  // Output: false
System.out.println(a || b);  // Output: true

 

Unary Operators

Unary operators are used with only one operand. They perform operations like incrementing, decrementing, negating, or inverting a value.

OperatorDescriptionExample
+Unary plus (positive value)+a
-Unary minus (negates value)-a
++Increment (increases value by 1)++a or a++
--Decrement (decreases value by 1)--a or a--
!Logical NOT (inverts the boolean value)!a

Example:

int a = 5;
System.out.println(++a);  // Output: 6 (pre-increment)
System.out.println(a--);  // Output: 6 (post-decrement, but now a is 5)
System.out.println(a);    // Output: 5

int b = -a;               // Unary minus
System.out.println(b);    // Output: -5

 

Bitwise Operators

Bitwise operators are used to perform operations on individual bits of integer values.

OperatorDescriptionExample
&Bitwise ANDa & b
|Bitwise ORa | b
^Bitwise XOR^a
~Bitwise NOT~a

Example:

int a = 5;  // 0101 in binary
int b = 3;  // 0011 in binary
System.out.println(a & b);  // Output: 1 (0001 in binary)
System.out.println(a | b);  // Output: 7 (0111 in binary)
System.out.println(a ^ b);  // Output: 6 (0110 in binary)
System.out.println(~a);     // Output: -6 (11111111 11111111 11111111 11111010 in binary)

 

Conditional (Ternary) Operator

The ternary operator is a shorthand for an if-else statement. It is the only operator in Java that takes three operands.

OperatorDescriptionExample
? :Ternary (conditional) operatorcondition ? expr1 : expr2

Example:

int a = 5;
int b = 10;
int max = (a > b) ? a : b;  // If a > b, max is a; otherwise, max is b
System.out.println(max);    // Output: 10

 

Shift Operators

Shift operators shift the bits of a number to the left or right.

OperatorDescriptionExample
<<Left shifta << 2
>>Right shifta >> 2
>>>Unsigned right shifta >>> 2
int a = 8;  // 1000 in binary
System.out.println(a << 1);  // Output: 16 (10000 in binary)

Left Shift Operator

The left shift operator shifts the bits of its operand to the left by the specified number of positions. The vacant positions on the right are filled with zeros.

Example:

int a = 5;   // Binary: 0101
int result = a << 2;  // Result: 10100 (decimal: 20)

Right Shift Operator

The signed right shift operator shifts the bits of its operand to the right. For positive numbers, the vacant positions on the left are filled with 0, while for negative numbers, they are filled with 1 to preserve the sign.

Example:

int a = 20;  // Binary: 00010100
int result = a >> 2;  // Result: 00000101 (decimal: 5)

int b = -8;  // Binary: 11111111 11111111 11111111 11111000
result = b >> 2;  // Result: 11111111 11111111 11111111 11111110 (decimal: -2)

Unsigned Right Shift Operator

The unsigned right shift operator (>>>) shifts the bits of a number to the right and fills the leftmost bits with zeros. It is used to shift the binary representation of an integer without preserving the sign (i.e., it treats the number as unsigned).

  • The >>> operator shifts the bits to the right and fills the leftmost bits with zeros.
  • Unlike the signed right shift (>>), which maintains the sign bit for negative numbers, >>> always inserts 0 in the leftmost positions.
  • This is particularly useful when working with binary representations of data.

Example:

int num = -8;             // Binary: 11111111 11111111 11111111 11111000
int result = num >>> 2;   // Shift right by 2 bits
System.out.println(result);  // Output: 1073741822

Explanation:

  • The binary representation of -8 is 11111111 11111111 11111111 11111000 (32-bit signed integer).
  • After shifting 2 bits to the right using >>>, the result becomes: 00111111 11111111 11111111 11111110, which is the decimal value 1073741822.

Unsigned Right Shift(>>>) vs Signed Right Shift (>>):

  • Signed right shift (>>): Preserves the sign bit (keeps negative numbers negative).
  • Unsigned right shift (>>>): Fills the leftmost bits with 0, ignoring the sign bit.

Example:

int num = -8;
System.out.println(num >> 2);   // Output: -2 (signed shift)
System.out.println(num >>> 2);  // Output: 1073741822 (unsigned shift)
Last updated on