In Java, there are several types of operators that can be used to perform various operations.
Here’s a table of the most commonly used operators, along with their syntax and examples:
Operator | Description | Syntax | Example |
---|---|---|---|
+ | Addition | <operand1> + <operand2> | int sum = 10 + 20; |
– | Subtraction | <operand1> - <operand2> | int difference = 20 - 10; |
* | Multiplication | <operand1> * <operand2> | int product = 10 * 20; |
/ | Division | <operand1> / <operand2> | int quotient = 20 / 10; |
% | Modulus (Remainder) | <operand1> % <operand2> | int remainder = 20 % 10; |
++ | Increment | <variable_name>++ | int x = 10; x++; |
— | Decrement | <variable_name>-- | int y = 10; y--; |
== | Equal to | <operand1> == <operand2> | boolean result = 10 == 20; |
!= | Not equal to | <operand1> != <operand2> | boolean result = 10 != 20; |
> | Greater than | <operand1> > <operand2> | boolean result = 10 > 20; |
< | Less than | <operand1> < <operand2> | boolean result = 10 < 20; |
>= | Greater than or equal to | <operand1> >= <operand2> | boolean result = 10 >= 20; |
<= | Less than or equal to | <operand1> <= <operand2> | boolean result = 10 <= 20; |
&& | Logical AND | <operand1> && <operand2> | boolean result = true && false; |
|| | Logical OR | <operand1> || <operand2> | boolean result = true || false; |
! | Logical NOT | !<operand> | boolean result = !true; |
Note that the increment (++
) and decrement (--
) operators can be used both as post-increment and pre-increment operators. For example, x++
increments the value of x
after it has been used in the expression, while ++x
increments the value of x
before it has been used in the expression. The same is true for the decrement operator (--
).
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
​ public class JavaOperatorsExample { public static void main(String[] args) { int x = 10; int y = 20; // Arithmetic operators int sum = x + y; int difference = y - x; int product = x * y; int quotient = y / x; int remainder = y % x; System.out.println("Sum: " + sum); System.out.println("Difference: " + difference); System.out.println("Product: " + product); System.out.println("Quotient: " + quotient); System.out.println("Remainder: " + remainder); // Increment and Decrement operators int z = x++; System.out.println("z (post-increment x): " + z); z = ++y; System.out.println("z (pre-increment y): " + z); z = x--; System.out.println("z (post-decrement x): " + z); z = --y; System.out.println("z (pre-decrement y): " + z); // Relational operators boolean isEqual = x == y; boolean isNotEqual = x != y; boolean isGreaterThan = x > y; boolean isLessThan = x < y; boolean isGreaterThanOrEqual = x >= y; boolean isLessThanOrEqual = x <= y; System.out.println("x == y: " + isEqual); System.out.println("x != y: " + isNotEqual); System.out.println("x > y: " + isGreaterThan); System.out.println("x < y: " + isLessThan); System.out.println("x >= y: " + isGreaterThanOrEqual); System.out.println("x <= y: " + isLessThanOrEqual); // Logical operators boolean isTrue = true; boolean isFalse = false; boolean resultAnd = isTrue && isFalse; boolean resultOr = isTrue || isFalse; boolean resultNot = !isTrue; System.out.println("true && false: " + resultAnd); System.out.println("true || false: " + resultOr); System.out.println("!true: " + resultNot); } } |
Output:
Sum: 30
Difference: 10
Product: 200
Quotient: 2
Remainder: 0
z (post-increment x): 10
z (pre-increment y): 21
z (post-decrement x): 11
z (pre-decrement y): 20
x == y: false
x != y: true
x > y: false
x < y: true x >= y: false
x <= y: true
true && false: false
true || false: true
!true: false