This article is about the use of operators in Java.
Operators are characters or symbols that tell the Java compiler to perform a specific mathematical or logical manipulation. Java has four types of operators, arithmetic, bitwise, relational, and logical. There is also the standard assignment operator as well.
Arithmetic Operators
Arithmetic operators are used with numerical values to perform different mathematical operations. Listed below are the Java Arithmetic Operators.
Name | Operator | Description |
---|---|---|
Addition | + | Sum of two numbers. |
Subtraction | – | Difference of two numbers. |
Multiplication | * | Product of two numbers. |
Division | / | Division of two numbers. |
Modulus | % | Remainder of the division of two numbers. |
increment | ++ | increments the number by 1 |
decrement | — | Decrements the number by 1 |
Assignment Operators
Arguably the most common, Assignment Operators are used to assign values to variables. See the full list of Assignment operations below.
Operator | Example | Equivalent |
---|---|---|
= | x = 2 | N/A |
+= | x += 2 | x = x + 2 |
-= | x -= 2 | x = x - 2 |
*= | x *= 2 | x = x * 2 |
/= | x /= 2 | x = x / 2 |
\= | x \= 2 | x = x \ 2 |
%= | x %= 2 | x = x % 3 |
^= | x ^= 2 | x = x ^ 2 |
Comparison Operators
Comparison Operators compare the contents of a variable to another. If the Comparison held true, then the Boolean value “True” is returned. However if the comparison was false, then the Boolean value “False” is returned.
Operator | Description |
---|---|
== | Equals to |
!= | Not Equal to |
> | Greater than |
>= | Greater than or equals to |
< | Less than |
<= | Less than or equals to |
Logical Operators:
Used to conduct Logical Operations between conditional statements.
Operator | Description |
---|---|
and | Returns True if both of the two statements was True. |
or | Returns True if one of the two statements was True. |
not | Inverses the result. |
This marks the end of the Operators in Java Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked below in the comment.