Operators in Java with Examples | Java Programming Guide
Diagram Operators in Java with Example
Introduction to Operators in Java
In Java, operators are special symbols or keywords used to perform operations on variables and values. They are fundamental building blocks of any Java program, allowing developers to perform arithmetic, comparisons, logical operations, and more.
Understanding Java operators is crucial for writing efficient and bug-free code. In this article, we will explore various types of operators in Java along with practical examples.
Java provides a rich set of operators categorized into the following types:
Arithmetic Operators
Relational (Comparison) Operators
Logical Operators
Assignment Operators
Unary Operators
Bitwise Operators
Ternary Operator
Let’s understand each with examples.
These operators perform basic mathematical operations.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus | a % b |
int a = 10, b = 3;
System.out.println("Addition: " + (a + b)); // 13
System.out.println("Modulus: " + (a % b)); // 1
These are used to compare two values.
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal | a >= b |
<= | Less than or equal | a <= b |
int a = 5, b = 10;
System.out.println(a > b); // false
System.out.println(a <= b); // true
Used to perform logical operations, usually with boolean values.
Operator | Description | Example |
---|---|---|
&& | Logical AND | a > 5 && b < 10 |
! | Logical NOT | !(a > 5) |
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 5 |
*= | Multiply and assign | a *= 5 |
/= | Divide and assign | a /= 5 |
%= | Modulus and assign | a %= 5 |
int a = 10;
a += 5; // a = a + 5
System.out.println(a); // 15
Operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | a++ |
-- | Decrement | a-- |
! | Logical NOT | !a |
int a = 5;
System.out.println(++a); // 6 (pre-increment)
System.out.println(a--); // 6 (post-decrement)
Perform operations on individual bits of numbers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
Bitwise OR | ||
^ | Bitwise XOR | a ^ b |
~ | Bitwise complement | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
int a = 5, b = 3;
System.out.println(a & b); // 1
System.out.println(a << 1); // 10
Short-hand for if-else statement.
Operator | Description | Syntax |
---|---|---|
?: | Ternary conditional | condition ? expr1 : expr2 |
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // 20
Understanding the different Java operators is essential for writing logic in your programs. Whether you are performing arithmetic, logical checks, or bitwise manipulations, Java operators make your code readable and efficient.
Keep practicing with different combinations and you'll gain better command over Java's operator system.
Q1: What is the difference between == and equals() in Java?==
checks reference equality, while equals()
checks value equality (especially with objects).
Q2: Is += faster than using a = a + b?
They are functionally the same, but +=
is more concise and preferred for readability.