Operator
Operators in Java are special symbols or keywords used to perform operations on variables and values. Java supports a wide range of operators for tasks like arithmetic calculations, comparison, logical operations, and more. Here's a detailed explanation of the various types of operators in Java:
1. Arithmetic Operators:Arithmetic operators are used for mathematical calculations.
+
(Addition): Adds two operands.-
(Subtraction): Subtracts the right operand from the left operand.*
(Multiplication): Multiplies two operands./
(Division): Divides the left operand by the right operand.%
(Modulus): Divides the left operand by the right operand and returns the remainder.
2. Relational Operators:Relational operators are used to compare two values.
==
(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.
3. Logical Operators:Logical operators are used to perform logical operations on boolean values.
&&
(Logical AND): Returns true if both conditions on the left and right are true.||
(Logical OR): Returns true if either of the conditions on the left or right is true.!
(Logical NOT): Reverses the logical state of its operand.
4. Assignment Operators:Assignment operators are used to assign values to variables.
=
(Assignment): Assigns the value on the right to the variable on the left.+=
(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 left operand with the right 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): Performs modulus on the left operand with the right operand and assigns the result to the left operand.
5. Unary Operators:Unary operators operate on a single operand.
++
(Increment): Increases the value of the operand by 1.--
(Decrement): Decreases the value of the operand by 1.+
(Positive): Indicates a positive value. (Usually, numbers are already positive, so this is rarely used.)-
(Negation): Negates the value of the operand.
6. Bitwise Operators:Bitwise operators perform operations on individual bits of a number.
&
(Bitwise AND): Performs a bitwise AND operation.|
(Bitwise OR): Performs a bitwise OR operation.^
(Bitwise XOR): Performs a bitwise XOR (exclusive OR) operation.~
(Bitwise NOT): Flips the bits of the operand.
7. Conditional (Ternary) Operator:The conditional operator (also known as the ternary operator) is a shorthand way of writing an
if-else
statement.? :
(Conditional Operator): Evaluates a condition and returns one of two values based on whether the condition is true or false.
Understanding and mastering these operators are essential for writing efficient and effective Java code. They enable developers to perform a wide array of operations, from basic arithmetic calculations to complex logical evaluations.
Last updated