bhylu.com

Advertisements

Java Arithmetic, Assignment and Precedence

Operators form expressions, and expressions produce values or side effects. Make the intended grouping obvious when a calculation contains several operations.

Understand the idea

Multiplication, division and remainder bind more tightly than addition and subtraction. Parentheses can express the intended grouping without making a reader remember a precedence table. Assignment changes a variable; comparison checks a relationship. Compound assignment such as += both calculates and assigns, with its own conversion rules. Keep increments and other side effects out of complicated conditions while learning.

Run it locally

Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.

JDK and terminal setup

javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out Main

A worked example

Java 21+ · Main.java

public class Main {
    public static void main(String[] args) {
        int groups = 3;
        int seatsPerGroup = 4;
        int extras = 2;
        int total = groups * seatsPerGroup + extras;
        total -= 1;
        System.out.println(total);
        System.out.println(total % seatsPerGroup);
    }
}

Expected output

13
1

The multiplication produces twelve before extras adds two. Removing one cancelled seat leaves thirteen. Remainder reports what is left after whole groups of four are taken from thirteen. The order is part of the expression, not determined by whichever variable name feels most important.

Check the official Java documentation for detailed contracts and supported options.