Java Operators

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 09 / 50 · English

Java Operators

Turn values into expressions.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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 true or false: == != > < >= <=
  • 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 / கோடின் விளக்கம்

CodeMeaning
intData type. The result of adding two ints is also an int.
resultVariable that stores the outcome of the expression.
=Assignment operator — stores the computed value into result.
aThe left operand — the first number in the operation.
+The arithmetic operator. Here it adds the values of a and b together.
bThe 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) — true only when both sides are true
  • || (OR) — true when 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

OperatorExampleEquivalent toMeaning
+=x += 5x = x + 5Add and assign
-=x -= 3x = x - 3Subtract and assign
*=x *= 2x = x * 2Multiply and assign
/=x /= 4x = x / 4Divide and assign
%=x %= 3x = x % 3Modulus and assign

Ternary Operator

The ternary operator ? : is a one-line shorthand for a simple if-else. Syntax: condition ? valueIfTrue : valueIfFalse

Java reference fragmentJAVA
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: 25
03 / Read and predict

A 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.

Main.java · complete runnable exampleJAVA
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.

04 / Learn by changing

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.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

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.

05 / Your turn

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.