Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 07

Operators in Java

In this lesson: Arithmetic, relational, logical, bitwise, assignment, unary and ternary operators in Java — with output. In Hindi & English.

What are operators

Operators are symbols that do things with values — add them, compare them, or combine conditions. They are the basic building blocks of every calculation and decision in a program. Java groups them into arithmetic, assignment, comparison, logical, and increment/decrement.

💡 Operators act on values: + adds, == compares, && combines conditions.

Arithmetic operators

int a = 10, b = 3;
System.out.println(a + b);   // 13
System.out.println(a - b);   // 7
System.out.println(a * b);   // 30
System.out.println(a / b);   // 3  (integer division)
System.out.println(a % b);   // 1  (remainder)
13
7
30
3
1

Note a / b with two ints gives 3, not 3.33 — integer division drops the decimal. % gives the remainder, great for even/odd checks.

Assignment operators

int x = 10;
x += 5;   // x = x + 5  -> 15
x -= 3;   // x = x - 3  -> 12
x *= 2;   // x = x * 2  -> 24
System.out.println(x);
24

= assigns a value. Shortcuts like += and *= update a variable using its own current value, saving typing.

Comparison operators

int a = 10, b = 3;
System.out.println(a == b);   // false
System.out.println(a != b);   // true
System.out.println(a > b);    // true
System.out.println(a <= b);   // false
false
true
true
false

These compare two values and give a boolean (true/false). Remember == compares, while a single = assigns.

Logical operators

int age = 20;
boolean ok = (age > 18) && (age < 60);   // AND
boolean any = (age < 12) || (age > 18);  // OR
System.out.println(ok);
System.out.println(!ok);                 // NOT
true
false

&& (AND) is true only if both sides are true. || (OR) is true if either side is. ! (NOT) flips true and false.

Increment and decrement

int i = 5;
System.out.println(i++);   // prints 5, then i becomes 6
System.out.println(++i);   // i becomes 7, then prints 7
5
7

i++ uses the value first, then adds 1. ++i adds 1 first, then uses it. Both are common in loops as a short way to count.

Common mistakes

  • Using = instead of ==. = assigns; == compares.
  • Integer division surprise. 5 / 2 is 2, not 2.5 — use a double for decimals.
  • Confusing i++ and ++i. The order of "use" and "add" differs.
  • Using & for conditions. Use && and || for logic, not the bitwise &.

Summary

  • Operators act on values for calculations and decisions.
  • Arithmetic: + - * / % (watch integer division).
  • Comparison returns true/false; == compares, = assigns.
  • Logical &&, ||, ! combine conditions.
  • ++ and -- increase or decrease by 1, with pre/post forms.

Next, make decisions with if-else in Java, or repeat actions with Java loops.

Frequently Asked Questions

What are operators in Java?
Operators are symbols that perform actions on values — like + to add, > to compare, and && to combine conditions. They are the basic tools for calculations and decisions in a program.
What does the % (modulus) operator do?
The modulus operator % gives the remainder after division. For example 10 % 3 is 1. It is commonly used to check even/odd numbers (n % 2 == 0) or to cycle through values.
What is the difference between = and ==?
A single = assigns a value to a variable. A double == compares two values and returns true or false. Mixing them up is a very common beginner error.
What is the difference between ++i and i++?
Both increase i by 1. ++i (pre-increment) increases first, then uses the value; i++ (post-increment) uses the current value first, then increases. The difference matters when used inside a bigger expression.
What is the difference between && and &?
&& is the logical AND for conditions and short-circuits (stops early if the first is false). A single & is a bitwise operator. For if-conditions, always use && and ||.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicType Casting in Java Your progress: 7 / 61 (11%) · Lesson 8 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free