Operators are symbols used to perform operations on variables and values in Java. They serve various purposes, from mathematical calculations to logical comparisons.
Operator Precedence: Operators in Java have a specific order of execution, known as precedence. Understanding this order is crucial to writing correct expressions.
Java divides the operators into the following groups:
Arithmetic operators allow you to perform mathematical operations like addition, subtraction, multiplication, division, and modulus (remainder).
Operator | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
-- | Decrement | Decreases the value of a variable by 1 | --x |
Assignment operators ( = ) are used to assign values to variables. Compound assignment operators combine an operation and assignment in one step.
A list of all assignment operators:
Operator | Example | Same As |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 6 | x = x + 6 |
-= | x-=9 | x=x-9 |
*= | x*=6 | x=x*6 |
/= | x/=6 | x=x/6 |
%= | x%=6 | x=x%6 |
&= | x&=6 | x=x&6 |
|= | x|=6 | x=x|6 |
>>= | x >>= 3 | x = x >> 3 |
<<= | x <<= 3 | x = x << 3 |
Comparison operators are used to compare values and return boolean results, indicating whether the comparison is true or false.
int x = 5; int y = 3; System.out.println(x > y); // output: true
Operator | Name | Example |
---|---|---|
== | Equal to | Example |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Logical operators are used to perform logical operations, like AND, OR, and NOT, primarily in conditional statements.
int a=5; int b=5; if( a==b && b==3){ System.out.println(" Abyantrik"); }else { System.out.println("Tutorials"); } // output: Tutorials
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |
Bitwise operators work on individual bits of integers. They are used in low-level programming and advanced operations.
Operators | Symbol | Example |
---|---|---|
Bitwise AND | & | x & y |
Bitwise OR | | | x | y |
Exclusive OR (XOR) | ^ | x ^ y |
Bitwise Left shift | << | x << y |
Bitwise Right shift | >> | x >> y |
The ternary operator is a shorthand way of writing if-else statements, allowing you to make decisions in a compact form. ( ? , :)
(condition) ? "Statement execute if true": "Statement execute if false";
The instanceof operator is used to determine the type of an object, helping you work with polymorphism and object-oriented programming.