Operators
Operators in Java
Operators are special symbols or keywords in Java that perform operations on variables and values. They are fundamental to performing calculations, making decisions, and manipulating data in your programs. In this section, we will explore the different types of operators in Java and provide examples of how they are used.
1. Types of Operators in Java
Java provides several types of operators, each serving a specific purpose. The main categories of operators in Java are:
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Unary Operators
Bitwise Operators
Ternary Operator
Shift Operators
Let's explore each type of operator in detail.
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division.
List of Arithmetic Operators:
+
Addition
a + b
-
Subtraction
a - b
*
Multiplication
a * b
/
Division
a / b
%
Modulus (remainder)
a % b
Example:
3. Relational Operators
Relational operators are used to compare two values. They return a boolean value (true
or false
) based on the comparison.
List of Relational Operators:
==
Equal to
a == b
!=
Not equal to
a != b
>
Greater than
a > b
<
Less than
a < b
>=
Greater than or equal to
a >= b
<=
Less than or equal to
a <= b
Example:
4. Logical Operators
Logical operators are used to combine multiple boolean expressions. They are often used in decision-making statements like if
and while
.
List of Logical Operators:
&&
Logical AND
a && b
| |
Logical OR
Logical OR
!
Logical NOT
!a
Example:
5. Assignment Operators
Assignment operators are used to assign values to variables. The most basic assignment operator is =
, but there are also compound assignment operators that perform an operation and assign the result to the variable.
List of Assignment Operators:
=
Assigns value to a variable
a = 10
+=
Adds and assigns
a += 5
-=
Subtracts and assigns
a -= 5
*=
Multiplies and assigns
a *= 5
/=
Divides and assigns
a /= 5
%=
Modulus and assigns
a %= 5
Example:
6. Unary Operators
Unary operators operate on a single operand. They are commonly used to increment, decrement, or negate values.
List of Unary Operators:
+
Unary plus (positive value)
+a
-
Unary minus (negates value)
-a
++
Increment
a++
or ++a
--
Decrement
a--
or --a
!
Logical NOT (negates boolean)
!a
Example:
7. Bitwise Operators
Bitwise operators perform operations on individual bits of integer values. They are primarily used in low-level programming, such as in embedded systems.
List of Bitwise Operators:
&
Bitwise AND
a & b
`
`
Bitwise OR
^
Bitwise XOR
a ^ b
~
Bitwise NOT
~a
Example:
8. Ternary Operator
The ternary operator is a shorthand way of writing an if-else
statement. It is the only operator in Java that takes three operands.
Syntax:
If
condition
is true,expression1
is evaluated and returned.If
condition
is false,expression2
is evaluated and returned.
Example:
9. Shift Operators
Shift operators are used to shift bits of a number to the left or right.
List of Shift Operators:
<<
Left shift
a << 2
>>
Right shift
a >> 2
>>>
Unsigned right shift
a >>> 2
Example:
Conclusion
Operators in Java are powerful tools that allow you to perform a wide range of operations, from basic arithmetic to complex logical and bitwise operations. By mastering these operators, you can write more efficient and effective Java code.
For more Java tutorials and resources, visit codeswithpankaj.com.
Last updated