Operators
- Operators in Java are special symbols that perform operations on variables and values.
- Java provides a rich set of operators to manipulate variables.
Types of Operator :
There are 7 types of operators in Java based on the type of operations they perform:
1. Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
- Addition (
+
): Adds two operands. - Subtraction (
-
): Subtracts the second operand from the first. - Multiplication (
*
): Multiplies two operands. - Division (
/
): Divides the numerator by the denominator. - Modulus (
%
): Returns the remainder of a division. - Increment (
++
): Increases the value of the operand by 1. - Decrement (
--
): Decreases the value of the operand by 1.
Example:
int a = 10, b = 5;
System.out.println(a + b); // 15
System.out.println(a - b); // 5
System.out.println(a * b); // 50
System.out.println(a / b); // 2
System.out.println(a % b); // 0
a++;
System.out.println(a); // 11
b--;
System.out.println(b); // 4
2. Relational Operators:
Relational operators are used to compare two values. They return a boolean result (true or false).
- Equal to (
==
): Checks if two operands are equal. - Not equal to (
!=
): Checks if two operands are not equal. - Greater than (
>
): Checks if the left operand is greater than the right operand. - Less than (
<
): Checks if the left operand is less than the right operand. - Greater than or equal to (
>=
): Checks if the left operand is greater than or equal to the right operand. - Less than or equal to (
<=
): Checks if the left operand is less than or equal to the right operand.
Example:
int a = 10, b = 5;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // true
System.out.println(a < b); // false
System.out.println(a >= b); // true
System.out.println(a <= b); // false
3. Logical Operators:
Logical operators are used to combine two or more conditions.
- Logical AND (
&&
): Returns true if both operands are true. - Logical OR (
||
): Returns true if at least one of the operands is true. - Logical NOT (
!
): Inverts the boolean value of the operand.
Example:
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
System.out.println(!x); // false
4. Bitwise Operators:
Bitwise operators perform bit-by-bit operations.
- Bitwise AND (
&
): Performs a bitwise AND operation. - Bitwise OR (
|
): Performs a bitwise OR operation. - Bitwise XOR (
^
): Performs a bitwise XOR operation. - Bitwise Complement (
~
): Inverts each bit of the operand. - Left Shift (
<<
): Shifts bits to the left. - Right Shift (
>>
): Shifts bits to the right. - Unsigned Right Shift (
>>>
): Shifts bits to the right without sign extension.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b); // 0001 (1)
System.out.println(a | b); // 0111 (7)
System.out.println(a ^ b); // 0110 (6)
System.out.println(~a); // 1010 (in binary, -6 in decimal)
System.out.println(a << 1); // 1010 (10)
System.out.println(a >> 1); // 0010 (2)
System.out.println(a >>> 1); // 0010 (2)
5. Assignment Operators:
Assignment operators are used to assign values to variables.
- Assignment (
=
): Assigns the value of the right operand to the left operand. - Add and assign (
+=
): Adds the right operand to the left operand and assigns the result to the left operand. - Subtract and assign (
-=
): Subtracts the right operand from the left operand and assigns the result to the left operand. - Multiply and assign (
*=
): Multiplies the right operand with the left operand and assigns the result to the left operand. - Divide and assign (
/=
): Divides the left operand by the right operand and assigns the result to the left operand. - Modulus and assign (
%=
): Takes modulus using two operands and assigns the result to the left operand.
Example:
int a = 10;
a += 5; // a = a + 5; => 15
a -= 3; // a = a - 3; => 12
a *= 2; // a = a * 2; => 24
a /= 4; // a = a / 4; => 6
a %= 3; // a = a % 3; => 0
6. Conditional (Ternary) Operator:
The ternary operator is a shorthand for the if-else
statement and is used to evaluate a boolean expression.
- Syntax:
condition ? expression1 : expression2
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println(max); // 20
7. Unary Operators:
Unary operators are used with only one operand.
- Unary plus (
+
): Indicates a positive value. - Unary minus (
-
): Negates an expression. - Increment (
++
): Increases the value of a variable by 1. - Decrement (
--
): Decreases the value of a variable by 1. - Logical complement (
!
): Inverts the value of a boolean expression.
Example:
int a = 5;
System.out.println(+a); // 5
System.out.println(-a); // -5
a++;
System.out.println(a); // 6
a--;
System.out.println(a); // 5
boolean b = true;
System.out.println(!b); // false
Understanding these operators and their proper usage is fundamental to programming in Java, as they form the basis for performing operations on data and making decisions in your code.