Java Programming, Operators · December 2, 2026

Java for Beginners, Lesson 15: Assignment, Ternary Operator and Operator Precedence in Java

Java for Beginners · Module 3: Operators


🎯 The Last Piece of the Operator Puzzle

You now know the arithmetic, comparison, logical and bitwise operators. This lesson covers three more things: the assignment operator, the ternary operator (a one-line if-else) and precedence, which decides which operator runs first when a line has several.

1️⃣ The Assignment Operator =

= copies the value on the right into the variable on the left. It is evaluated right to left, so you can chain it.

public class AssignDemo {
    public static void main(String[] args) {
        int a, b, c;
        a = b = c = 7;
        System.out.println("a = " + a + ", b = " + b + ", c = " + c);

        int x = 10;
        int y = x;
        x = 99;
        System.out.println("x = " + x + ", y = " + y);

        int total = 0;
        total += 5;
        total += 7;
        System.out.println("total = " + total);
    }
}
🖥️ Output:
a = 7, b = 7, c = 7
x = 99, y = 10
total = 12
  • a = b = c = 7 works from the right: c gets 7, then b gets the value of that, then a.
  • y = x copies the value. Later changing x does not change y.
  • The left side of = must be a variable. Compound forms like += were covered in Lesson 12.

2️⃣ The Ternary Operator ? :

(marks >= 40)
condition
?
"Pass"
if true
:
"Fail"
if false
Syntax: condition ? valueIfTrue : valueIfFalse. It is the only operator in Java with three operands, so it is called the ternary operator.

Figure 1: The conditional (ternary) operator.

Program 2: Choosing a value in one line

public class TernaryDemo {
    public static void main(String[] args) {
        int marks = 62;
        String result = (marks >= 40) ? "Pass" : "Fail";
        System.out.println("Result: " + result);

        int a = 14, b = 29;
        int max = (a > b) ? a : b;
        System.out.println("Max = " + max);

        int n = 7;
        System.out.println(n + " is " + (n % 2 == 0 ? "even" : "odd"));

        int score = 85;
        String grade = score >= 90 ? "A" : score >= 75 ? "B" : score >= 50 ? "C" : "D";
        System.out.println("Grade: " + grade);
    }
}
🖥️ Output:
Result: Pass
Max = 29
7 is odd
Grade: B
  • The test runs first. If it is true, the value before the colon is the answer. Otherwise the value after it is.
  • It produces a value, so you can store it in a variable or use it inside println.
  • Ternaries can be nested, as in the grade example. Beyond two levels they become hard to read, so use if-else (Lesson 16) instead.
  • Both possible results must have compatible types.

3️⃣ Operator Precedence and Associativity

In 2 + 3 * 4, does Java add first or multiply first? Precedence is the priority order of operators. Higher priority runs first. When two operators have the same priority, associativity decides: most go left to right.

2 + 3 * 4
* first
2 + 12
then +
14
(2 + 3) * 4
brackets first
5 * 4
then *
20

Figure 3: Precedence decides the order. Brackets override it.

PriorityOperatorsDirection
1 (highest)() brackets, x++ x--left to right
2++x --x ! ~ unary + -right to left
3* / %left to right
4+ -left to right
5<< >> >>>left to right
6< <= > >=left to right
7== !=left to right
8&left to right
9^left to right
10|left to right
11&&left to right
12||left to right
13? :right to left
14 (lowest)= += -= *= and other assignmentsright to left

Figure 2: Operator precedence, from highest to lowest priority.

Program 3: Precedence at work

public class Precedence {
    public static void main(String[] args) {
        System.out.println("2 + 3 * 4       = " + (2 + 3 * 4));
        System.out.println("(2 + 3) * 4     = " + ((2 + 3) * 4));
        System.out.println("20 - 6 - 3      = " + (20 - 6 - 3));
        System.out.println("100 / 10 * 2    = " + (100 / 10 * 2));
        System.out.println("10 + 5 % 3      = " + (10 + 5 % 3));
        System.out.println("1 + 2 + \"3\"     = " + (1 + 2 + "3"));

        boolean r = true || false && false;
        System.out.println("true || false && false = " + r);
        boolean r2 = (true || false) && false;
        System.out.println("(true || false) && false = " + r2);

        int a = 5;
        int b = a++ * 2 + 3;
        System.out.println("a++ * 2 + 3 with a=5 -> b = " + b + ", a = " + a);

        int p, q;
        p = q = 4 + 2 * 3;
        System.out.println("p = q = 4 + 2 * 3 -> " + p + ", " + q);
    }
}
🖥️ Output:
2 + 3 * 4 = 14
(2 + 3) * 4 = 20
20 – 6 – 3 = 11
100 / 10 * 2 = 20
10 + 5 % 3 = 12
1 + 2 + “3” = 33
true || false && false = true
(true || false) && false = false
a++ * 2 + 3 with a=5 -> b = 13, a = 6
p = q = 4 + 2 * 3 -> 10, 10
  • * beats +, so 2 + 3 * 4 is 14. Brackets change the order and give 20.
  • Same priority goes left to right: 20 - 6 - 3 is (20 – 6) – 3 = 11, and 100 / 10 * 2 is (100 / 10) * 2 = 20.
  • % has the same priority as *, higher than +: 10 + (5 % 3) = 12.
  • 1 + 2 + "3" adds the numbers first (3), then joins the text, giving “33”.
  • && beats ||, so true || false && false is true || (false && false), which is true.
  • a++ * 2 + 3 uses the old a (5): 5 × 2 + 3 = 13, and then a becomes 6.
  • Assignment has the lowest priority and goes right to left, so the whole right side is calculated before it is stored.
💡 You do not need to memorise the table. When in doubt, use brackets. They make your intention clear to anyone reading the code, including you next month.

4️⃣ Errors Beginners Meet

Mismatched types in a ternary

public class Bad1 {
    public static void main(String[] args) {
        int a = 3, b = 8;
        int max = (a > b) ? "a" : b;
        System.out.println(max);
    }
}
🖥️ Compiler message:
Bad1.java:4: error: incompatible types: bad type in conditional expression
int max = (a > b) ? “a” : b;
^
String cannot be converted to int
1 error
  • The result must fit the variable, so both choices must be ints here.

Assigning to a value

public class Bad2 {
    public static void main(String[] args) {
        int x = 5;
        10 = x;
        System.out.println(x);
    }
}
🖥️ Compiler message:
Bad2.java:4: error: unexpected type
10 = x;
^
required: variable
found: value
1 error
  • The variable goes on the left and the value on the right: x = 10;.

⚠️ Common Mistakes

MistakeProblemFix
Assuming a + b * c adds first* is higher priorityUse brackets (a + b) * c
Long nested ternariesUnreadableUse if-else
Different types in the two ternary resultsCompile error or surprise conversionKeep both the same type
5 = xLeft side is not a variablex = 5
Writing 1 + 2 + "3" expecting “123”Numbers are added firstStart with a String: "" + 1 + 2 + 3

❓ Frequently Asked Questions

Q1. Is the ternary operator better than if-else?
It is shorter for choosing one of two values. For anything with several statements, if-else is clearer.

Q2. What does associativity mean?
It is the direction used when operators have the same priority. Most go left to right. Assignment and the ternary go right to left.

Q3. Do brackets slow the program down?
No. Brackets only affect how the compiler reads the expression.

Q4. Is = an operator?
Yes. It produces a value too, which is why a = b = 5 works.

✅ Key Takeaways

  • = stores the right-hand value in the left-hand variable, and chains from right to left.
  • condition ? A : B is a one-line choice between two values.
  • Order of work: brackets, then * / %, then + -, then comparisons, then &&, then ||, then assignment.
  • Same-priority operators usually run left to right.
  • Use brackets whenever the order is not obvious.

🔁 Module 3 Recap

Lessons 12 to 15 covered arithmetic, relational, logical, bitwise, shift, assignment and ternary operators. With these you can write any expression, and next you will use them to make decisions with if and switch in Module 4.

🎓 Practice questions: (1) Predict, then check: int r = 10 + 20 / 5 * 2 - 3; (2) Use the ternary operator to find the largest of three numbers. (3) What is the value of 5 > 3 && 2 > 4 || 1 < 2? (4) Rewrite a + b * c - d / e with brackets that show the actual order. (5) What does "" + 1 + 2 + 3 print, and why?
All 30 lessons in this series (open course page)

Module 1 · Getting Started

Module 2 · Building Blocks

Module 3 · Operators

Module 4 · Control Flow

Module 5 · Data and Methods

Module 6 · Objects and Revision