Java Operators

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:

OperatorDescriptionSyntaxExample
+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:

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top