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.
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.
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: && || !
| left | right | && (and) | || (or) |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
&& 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
| Operator | Remember |
|---|---|
/ and % | Two ints: whole part and remainder. int / 0 throws |
+=, *=... | Update in place; =+ is a typo trap |
a++ / ++a | Use then add / add then use |
| Precedence | * / before + -; brackets first |
== vs = | Compare vs assign |
&& || | Short-circuit; guard on the left |
? : | Pick one of two values |
StudyEasy AI Community
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
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.
