Java Operators Explained – A Complete Guide
Java Operators Explained: A Complete Guide for Beginners
๐ Introduction
Operators are the foundation of Java programming. Whether you're performing mathematical operations, comparing values, or making decisions in your code — Java operators are essential tools in every Java developer’s toolkit.
๐งฎ Types of Java Operators
Java provides several categories of operators:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Unary Operators
- Bitwise Operators
- Ternary Operator
- Instanceof Operator
๐ข 1. Arithmetic Operators
Operator | Description | Example | Output |
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 10 / 2 | 5 |
% | Modulus | 10 % 3 | 1 |
int a = 10, b = 3;
System.out.println("Addition: " + (a + b));
⚖️ 2. Relational (Comparison) Operators
Operator | Description | Example |
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
int a = 5, b = 10;
System.out.println(a < b); // true
๐ง 3. Logical Operators
Operator | Description | Example |
&& | Logical AND | a > 5 && b < 10 |
|| | Logical OR | a > 5 || b < 5 |
! | Logical NOT | !(a > 5) |
boolean result = (a > 5 && b < 10);
✍️ 4. Assignment Operators
Operator | Example | Equivalent To |
= | a = 5 | — |
+= | a += 3 | a = a + 3 |
-= | a -= 2 | a = a - 2 |
int a = 10;
a += 5; // a = 15
➕ 5. Unary Operators
+a
: Unary plus
-a
: Unary minus
++a / a++
: Increment
--a / a--
: Decrement
!a
: Logical NOT
⚙️ 6. Bitwise Operators
&
: Bitwise AND
|
: Bitwise OR
^
: Bitwise XOR
~
: Bitwise Complement
<<
: Left shift
>>
: Right shift
❓ 7. Ternary Operator
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Max: " + max);
๐งฌ 8. instanceof Operator
String str = "Hello";
System.out.println(str instanceof String); // true
๐ฏ Java Operators – Interview Questions
- What is the difference between
==
and .equals()
in Java?
- How does the
&&
operator differ from &
?
- Explain the use of the ternary operator with an example.
- What is the output of
10 + 20 + "Java"
in Java?
- How do pre-increment (
++a
) and post-increment (a++
) behave differently?
- What are bitwise operators and when are they used?
- Can the modulus operator (%) be used with floating-point numbers in Java?
- What is the use of
instanceof
in Java?
๐ Conclusion
Java operators are essential tools for building logic and functionality in your applications. Understanding them deeply helps you write better, cleaner, and more efficient code.
๐ฌ Comments Section
Join the Discussion
๐ What’s your experience with Java operators?
Leave a comment below and let's learn together! ๐ง๐ป