⬅ Previous Topic
Java Data TypesNext Topic ⮕
Java Operator Precedence⬅ Previous Topic
Java Data TypesNext Topic ⮕
Java Operator PrecedenceOperators in Java act like bridges between variables and values. They perform computations, comparisons, assignments, and logical decisions. Understanding these operators is fundamental before you dive into conditions, loops, and more complex logic in Java.
In this guide, we'll walk through all the primary types of Java operators, explaining each with easy-to-understand code examples and detailed output explanations.
Arithmetic operators are used to perform basic mathematical operations.
Operator | Symbol | Description |
---|---|---|
Addition | + | Adds two values |
Subtraction | - | Subtracts right from left |
Multiplication | * | Multiplies two values |
Division | / | Divides left by right |
Modulus | % | Returns remainder |
public class ArithmeticExample {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("a / b = " + (a / b));
System.out.println("a % b = " + (a % b));
}
}
a + b = 13
a - b = 7
a * b = 30
a / b = 3
a % b = 1
These operators assign values to variables.
Operator | Symbol | Description |
---|---|---|
Assign | = | Assigns right side to left variable |
Add and Assign | += | Performs addition then assigns |
Subtract and Assign | -= | Performs subtraction then assigns |
Multiply and Assign | *= | Performs multiplication then assigns |
Divide and Assign | /= | Performs division then assigns |
Modulus and Assign | %= | Performs modulus then assigns |
public class AssignmentExample {
public static void main(String[] args) {
int a = 5;
a += 3;
a *= 2;
System.out.println("Final value of a: " + a);
}
}
Final value of a: 16
Relational operators compare two values and return a boolean result.
Operator | Symbol | Description |
---|---|---|
Equal To | == | Checks if two values are equal |
Not Equal To | != | Checks if two values are not equal |
Greater Than | > | Checks if left is greater |
Less Than | < | Checks if left is smaller |
Greater Than or Equal | >= | Checks if left is greater or equal |
Less Than or Equal | <= | Checks if left is smaller or equal |
public class RelationalExample {
public static void main(String[] args) {
int x = 10, y = 20;
System.out.println("x == y: " + (x == y));
System.out.println("x != y: " + (x != y));
System.out.println("x < y: " + (x < y));
System.out.println("x > y: " + (x > y));
}
}
x == y: false
x != y: true
x < y: true
x > y: false
Logical operators help in combining multiple boolean expressions.
Operator | Symbol | Description |
---|---|---|
Logical AND | && | True if both operands are true |
Logical OR | || | True if any operand is true |
Logical NOT | ! | Inverts the boolean value |
public class LogicalExample {
public static void main(String[] args) {
int a = 5, b = 10;
System.out.println((a > 0) && (b > 0));
System.out.println((a > 0) || (b < 0));
System.out.println(!(a == b));
}
}
true
true
true
Unary operators operate on a single operand.
Operator | Symbol | Description |
---|---|---|
Unary Plus | + | Indicates positive value |
Unary Minus | - | Negates the value |
Increment | ++ | Increases value by 1 |
Decrement | -- | Decreases value by 1 |
Logical NOT | ! | Inverts a boolean |
public class UnaryExample {
public static void main(String[] args) {
int a = 5;
System.out.println("a = " + a);
System.out.println("++a = " + (++a));
System.out.println("a-- = " + (a--));
System.out.println("Final a = " + a);
}
}
a = 5
++a = 6
a-- = 6
Final a = 5
Bitwise operators work on bits and perform bit-by-bit operations.
Operator | Symbol | Description |
---|---|---|
AND | & | Bitwise AND |
OR | | | Bitwise OR |
XOR | ^ | Bitwise exclusive OR |
Complement | ~ | Bitwise NOT |
Left Shift | << | Shift bits left |
Right Shift | >> | Shift bits right |
public class BitwiseExample {
public static void main(String[] args) {
int a = 5, b = 3;
System.out.println("a & b: " + (a & b));
System.out.println("a | b: " + (a | b));
System.out.println("a ^ b: " + (a ^ b));
System.out.println("~a: " + (~a));
System.out.println("a << 1: " + (a << 1));
System.out.println("a >> 1: " + (a >> 1));
}
}
a & b: 1
a | b: 7
a ^ b: 6
~a: -6
a << 1: 10
a >> 1: 2
int a = 10, b = 3;
System.out.println("a % b = " + (a % b));
a += 3
is equivalent to a = a + 3
in Java.
int a = 5;
System.out.println(++a);
System.out.println(a--);
System.out.println(a);
&&
performs a bitwise AND operation in Java.⬅ Previous Topic
Java Data TypesNext Topic ⮕
Java Operator PrecedenceYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.