Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Basics

Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary

Values go in, one answer comes out. Six small programs cover arithmetic and why 100 / 3 is 33, compound assignment, postfix vs prefix, precedence, comparisons, && || ! with short-circuiting, and the ternary.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Basics

What you will learn

  • Use + - * / % and explain whole-number division and the remainder
  • Update a variable with += and friends, and tell a++ from ++a
  • Predict evaluation order with precedence and brackets
  • Combine conditions with && || ! and rely on short-circuit evaluation

Before you start

  • Java primitive types and memory
  • Java strings and text blocks

You have variables; now you want to do something with them, like multiply a price by a quantity. That's an operator's job. Values go in, one answer comes out: price * quantity with 120 and 3 gives 360, which you store in total. A piece of code that produces a value like this is an expression.

Arithmetic: + - * / %

package org.studyeasy.s02l06;

public class Ex01_Arithmetic {
    public static void main(String[] args) {
        int price = 120;
        int quantity = 3;
        int total = price * quantity;
        System.out.println("Total: " + total);
        System.out.println("7 % 3 = " + (7 % 3));
        System.out.println("100 / 3 = " + (100 / 3));
        System.out.println("100 % 3 = " + (100 % 3));
        System.out.println("100.0 / 3 = " + (100.0 / 3));
        System.out.println("5.0 / 0 = " + (5.0 / 0));
    }
}
Total: 360
7 % 3 = 1
100 / 3 = 33
100 % 3 = 1
100.0 / 3 = 33.333333333333336
5.0 / 0 = Infinity

% is modulo: the remainder. 7 divided by 3 leaves 1.

Now the line that catches everyone: 100 / 3 is 33, not 33.33. When both numbers are integers, the result is an integer and the decimal part is dropped. Make one side a decimal (100.0) and you keep the fraction. A decimal divided by zero gives Infinity, but an int divided by zero (5 / 0) compiles and then stops the run with ArithmeticException: / by zero.

100 split into groups of 3: three groups of 33 make 99, with 1 left over. 100 / 3 gives 33, the whole part; 100 % 3 gives 1, the leftover. 33 times 3 plus 1 equals 100. 3333331 100 / 3 = 33 (three groups) 100 % 3 = 1 33 x 3 + 1 = 100: int division keeps the whole part, modulo keeps the leftover.
With two ints, / gives the whole groups and % gives what's left over.

Update a variable in place

package org.studyeasy.s02l06;

public class Ex02_Compound {
    public static void main(String[] args) {
        int score = 10;
        score += 5;
        System.out.println("score += 5  -> " + score);
        score *= 2;
        System.out.println("score *= 2  -> " + score);
        score -= 4;
        System.out.println("score -= 4  -> " + score);
        score /= 4;
        System.out.println("score /= 4  -> " + score);
        score %= 4;
        System.out.println("score %= 4  -> " + score);
    }
}
score += 5  -> 15
score *= 2  -> 30
score -= 4  -> 26
score /= 4  -> 6
score %= 4  -> 2

score += 5 means score = score + 5. The others follow the same pattern. Note 26 / 4 is 6 (whole-number division again), and 6 % 4 leaves 2. Watch the typo score =+ 5;: it compiles, but it sets score to plus 5 instead of adding.

a++ or ++a

package org.studyeasy.s02l06;

public class Ex03_IncDec {
    public static void main(String[] args) {
        int a = 5;
        int b = a++;
        System.out.println("postfix: a = " + a + ", b = " + b);

        a = 6;
        int c = ++a;
        System.out.println("prefix:  a = " + a + ", c = " + c);
    }
}
postfix: a = 6, b = 5
prefix:  a = 7, c = 7

Both add one to a. The difference is which value the other variable gets. Postfix a++ uses the value first, then adds: b gets 5, then a becomes 6. Prefix ++a adds first, then uses: a becomes 7, and 7 is copied into c.

Precedence and comparisons

package org.studyeasy.s02l06;

public class Ex04_Precedence {
    public static void main(String[] args) {
        int plain = 2 + 3 * 4;
        int grouped = (2 + 3) * 4;
        System.out.println("2 + 3 * 4   = " + plain);
        System.out.println("(2 + 3) * 4 = " + grouped);
        System.out.println("18 >= 18    : " + (18 >= 18));
        System.out.println("7 != 7      : " + (7 != 7));
    }
}
2 + 3 * 4   = 14
(2 + 3) * 4 = 20
18 >= 18    : true
7 != 7      : false

Like maths, multiply and divide happen before add and subtract, so 2 + 3 * 4 is 2 + 12 = 14. Brackets change the order: (2 + 3) * 4 is 20. Brackets also make code easier to read.

A comparison always gives back true or false. Java has six: <, >, <=, >=, == (equal) and != (not equal). Two equals signs compare; one equals sign assigns.

A related trap: "Sum: " + 2 + 3 prints Sum: 23, because + runs left to right and joins text first. Write "Sum: " + (2 + 3).

Combine conditions: && || !

leftright&& (and)|| (or)
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

&& needs both sides true. || is false only when both sides are false. ! flips a value.

package org.studyeasy.s02l06;

public class Ex05_Logic {
    public static void main(String[] args) {
        int age = 20;
        boolean hasTicket = true;
        boolean isVip = false;

        System.out.println("allowed: " + (age >= 18 && hasTicket));
        System.out.println("front row: " + (isVip || age >= 18));
        System.out.println("no ticket: " + !hasTicket);

        int count = 0;
        int total = 300;
        boolean high = count != 0 && total / count > 50;
        System.out.println("average above 50: " + high);
    }
}
allowed: true
front row: true
no ticket: false
average above 50: false

&& stops as soon as it knows

The last line divides by count, which is 0, yet nothing crashes. With &&, if the left side is false the answer is already false, so Java never evaluates the right side. That's short-circuit evaluation. || does the same the other way round: a true left side skips the right.

Order matters. Swap the sides to total / count > 50 && count != 0 and the division runs first: ArithmeticException. Put the guard on the left.

Pick a value with ? :

package org.studyeasy.s02l06;

public class Ex06_Ternary {
    public static void main(String[] args) {
        int age = 20;
        String label = age >= 18 ? "Adult" : "Minor";
        System.out.println("label: " + label);

        int items = 3;
        System.out.println("You have " + items + (items == 1 ? " item" : " items"));
    }
}
label: Adult
You have 3 items

Read it as "condition ? value if true : value if false". Age is 20, so label is Adult. Inside a longer line, keep the brackets around the ternary: without them the + joins first and javac complains about bad operand types for ==.

Recap

OperatorRemember
/ and %Two ints: whole part and remainder. int / 0 throws
+=, *=...Update in place; =+ is a typo trap
a++ / ++aUse then add / add then use
Precedence* / before + -; brackets first
== vs =Compare vs assign
&& ||Short-circuit; guard on the left
? :Pick one of two values

Keep going in Java · 8 of 79

Previous
Java Strings and Text Blocks: Quotes, Concatenation and parseInt
Next post
Java Type Casting: Widening, Narrowing and Converting Text to Numbers
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program

Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.

Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA

Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.

Java Hello World with Maven: What the JDK Does and What Maven Adds

javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.

Java Variables, Keywords and Naming Rules

A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.

On this page

  • Arithmetic: + - * / %
  • Update a variable in place
  • a++ or ++a
  • Precedence and comparisons
  • Combine conditions: && || !
  • && stops as soon as it knows
  • Pick a value with ? :
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.