Java Operators
Turn values into expressions.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Operators.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
An expression combines values and operators to produce a result. The types of the operands matter: dividing two ints uses integer division, even if you assign the result to a double afterwards. Parentheses make your intended order explicit.
Explore the details
Java Operators
Operators are symbols that tell Java to perform an operation on variables or values. Java groups them into four main categories:
- Arithmetic — perform math:
+-*/%++-- - Comparison — compare two values, always returns
trueorfalse:==!=><>=<= - Logical — combine boolean expressions:
&&||! - Assignment — assign or update a variable's value:
=+=-=*=/=%=
Arithmetic Operators
Arithmetic operators perform standard math operations between two values. The % (modulus) operator returns the remainder after division — useful for checking if a number is even or odd.
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
int | Data type. The result of adding two ints is also an int. |
result | Variable that stores the outcome of the expression. |
= | Assignment operator — stores the computed value into result. |
a | The left operand — the first number in the operation. |
+ | The arithmetic operator. Here it adds the values of a and b together. |
b | The right operand — the second number in the operation. |
; | Semicolon ends the statement. |
Comparison Operators
Comparison operators compare two values and always produce a boolean result — either true or false. They are the building blocks of conditions in if statements and loops.
Don't confuse == (compare) with = (assign). Using = where you meant == is one of the most common beginner mistakes in Java.
Logical Operators
Logical operators are used to combine or invert boolean expressions. They are essential for writing complex conditions:
&&(AND) —trueonly when both sides are true||(OR) —truewhen at least one side is true!(NOT) — inverts the boolean value:!true→false
Assignment Operators
Assignment operators are shorthand for updating a variable. Instead of writing x = x + 5, you can write x += 5. They work for all arithmetic operations.
Compound Assignment Operators
| Operator | Example | Equivalent to | Meaning |
|---|---|---|---|
+= | x += 5 | x = x + 5 | Add and assign |
-= | x -= 3 | x = x - 3 | Subtract and assign |
*= | x *= 2 | x = x * 2 | Multiply and assign |
/= | x /= 4 | x = x / 4 | Divide and assign |
%= | x %= 3 | x = x % 3 | Modulus and assign |
Ternary Operator
The ternary operator ? : is a one-line shorthand for a simple if-else. Syntax: condition ? valueIfTrue : valueIfFalse
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
// Same logic written as if-else:
if (age >= 18) {
status = "Adult";
} else {
status = "Minor";
}
// Another example — find the larger number
int a = 10, b = 25;
int max = (a > b) ? a : b;
System.out.println("Max: " + max); // Max: 25A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int total = 17;
int groupSize = 5;
System.out.println(total / groupSize);
System.out.println(total % groupSize);
System.out.println((double) total / groupSize);
}
}Predict first. Then reveal the expected output / வெளியீடு
3 2 3.4
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Change total to 20 and predict the quotient and remainder.
Reveal the answer and reasoning / விடை
The integer quotient is 4, the remainder is 0, and the decimal division produces 4.0.
One mistake to avoid: Use == for primitive equality checks, not =. Use equals when comparing String contents.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.