Operators In Java
Operators allow us to perform various operations on data, manipulate values, and make our programs more dynamic.
Operators are symbols or special keywords that perform operations on one or more operands. Java provides a wide range of operators, including arithmetic, assignment, comparison, logical, and more that allow us to manipulate data, perform calculations, and create dynamic programs.
Let's explore each Java operation with practical example in detail and see how it works.
Arithmetic operators
Arithmetic operators perform basic mathematical calculations. We have familiar operators like addition, subtraction (-), multiplication (*), and division (/). Additionally, Java offers the modulus operator (%), which gives us the remainder of a division.
The increment operator (++) adds 1 to a variable, effectively incrementing its value.
The decrement operator (--) subtracts 1 from a variable, effectively decrementing its value.
Assignment operators
Assignment operators are used to assign values to variables. The most common one is the equals sign (=). However, Java also provides compound assignment operators like +=, -=, *=, and /=, which combine assignment with an arithmetic operation.
Comparison operators
Comparison operators allow us to compare values and determine relationships. We have operators such as equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators return a boolean value, either true or false. For example the equal to operator (==) compares two values for equality and returns true if they're equal.
Logical operators
Logical operators are used to combine multiple conditions and make decisions based on the results. We have three logical operators: AND (&&), OR (||), and NOT (!). They help us create complex conditions and control the flow of our programs.
The AND operator (&&) returns true if both conditions it connects are true.
The OR operator (||) returns true if either of the condition it connects is true.
The NOT operator returns true if the condition it connects is false.
Bitwise operators
Bitwise operators work at the bit level, manipulating individual bits in binary numbers. They are used in low-level programming and operations involving binary data. Some bitwise operators include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).