By WebNest Studio Engineering Team
Java - Core Tutorial
Java Operators
Operators are special symbols that perform operations on one, two, or three operands and return a result. Java groups operators by the kind of work they do, and understanding each group's precedence and behavior is essential for writing correct expressions.
Why it matters
Operators are where most small logic errors live: an integer division that drops the remainder, a condition that evaluates something it should have skipped, or == used where equals() was needed. These are easy to write, hard to spot in review, and they are favourite material for "what does this print?" interview questions.
Arithmetic Operators
+ - * / % perform addition, subtraction, multiplication, division, and modulus (remainder). Integer division truncates toward zero (7 / 2 is 3, not 3.5), while division involving at least one floating-point operand produces a decimal result.
Relational and Logical Operators
Relational operators (== != > < >= <=</code>) compare two values and produce a boolean. Logical operators (&& || !) combine boolean expressions. && and || are short-circuiting: the right-hand side is not evaluated if the result is already determined by the left-hand side, which is important when the right side has side effects.
Assignment and Compound Assignment
= assigns a value. Compound assignment operators like += -= *= /= %= combine an operation with assignment, and they also perform an implicit narrowing cast — byte b = 10; b += 5; compiles even though b = b + 5; would not.
Increment, Decrement, Ternary, and Bitwise Operators
++ and -- increase or decrease a value by 1, with prefix (++x) applying before use and postfix (x++) applying after use in an expression. The ternary operator condition ? valueIfTrue : valueIfFalse is a compact conditional expression. Bitwise operators (& | ^ ~ << >> >>>) operate directly on the binary representation of integer types.
Example
Arithmetic, short-circuit logic, and prefix vs postfix in one program
Java
public class OperatorsDemo {
public static void main(String[] args) {
System.out.println(7 / 2); // integer division
System.out.println(7.0 / 2); // floating-point division
System.out.println(7 % 2); // modulus
int x = 5;
boolean result = (x > 0) || (10 / 0 == 0); // short-circuit, avoids division by zero
System.out.println(result);
int a = 5;
System.out.println(a++); // prints 5, then a becomes 6
System.out.println(++a); // a becomes 7, then prints 7
int score = 85;
String grade = score >= 90 ? "A" : score >= 75 ? "B" : "C";
System.out.println(grade);
}
}
Output
3
3.5
1
true
5
7
B
Common Mistakes
- Using "=" (assignment) when "==" (comparison) was intended inside an if condition.
- Expecting "7 / 2" to give 3.5 in Java — integer division always truncates when both operands are integers.
- Confusing prefix (++a) and postfix (a++) behavior inside larger expressions, leading to off-by-one bugs.
- Relying on evaluation order of "&" and "|" (non-short-circuit) when short-circuit behavior ("&&", "||") was actually needed to avoid a side effect or exception.
Exercise
Write the leap-year rule using the modulus and logical operators. A year is a leap year if it is divisible by 4 and not by 100, unless it is also divisible by 400.
Expected output: 2024 true, 1900 false, 2000 true, each on its own line.
Java · starter code
public class LeapYear {
static boolean isLeap(int year) {
// TODO: return true when year is a leap year
return false;
}
public static void main(String[] args) {
System.out.println("2024 " + isLeap(2024));
System.out.println("1900 " + isLeap(1900));
System.out.println("2000 " + isLeap(2000));
}
}
Show hints
year % 4 == 0is true when year is divisible by 4.Combine the three tests with&&and||, using parentheses to make the grouping explicit.
Show solution
Java · solution
public class LeapYear {
static boolean isLeap(int year) {
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
public static void main(String[] args) {
System.out.println("2024 " + isLeap(2024));
System.out.println("1900 " + isLeap(1900));
System.out.println("2000 " + isLeap(2000));
}
}
Quick quiz
1. What does 5 / 2 evaluate to in Java?
A. 2.5B. 3C. 2D. 2.0
Show answer
C. Both operands are ints, so this is integer division, which truncates the result to 2.
2. Given int a = 5;, what is the value of a++ + ++a?
A. 10B. 11C. 13D. 12
Show answer
D. a++ yields 5 and then a becomes 6. ++a makes a 7 and yields 7. 5 + 7 = 12.
3. What happens when Java evaluates false && (10 / 0 == 0)?
A. It evaluates to false without dividingB. It throws ArithmeticExceptionC. It evaluates to trueD. It does not compile
Show answer
A. && short-circuits: the left side is false, so the right side is never evaluated and no division happens.
Interview questions
What is the difference between == and equals()?
Show answer
For primitives, == compares values. For objects, == compares references — whether both variables point to the same object — while equals() compares content, as defined by the class.
So two different String objects with the same characters are equal with equals() but may not be with ==. Use equals() to compare the content of objects.
What is the difference between >> and >>>?
Show answer
Both shift bits to the right. >> is the signed shift: it fills the vacated high bits with the sign bit, so a negative number stays negative. >>> is the unsigned shift: it always fills with zeros, so a negative int becomes a large positive number.
Key Points to Remember
- Integer division truncates; involve a double/float operand to get decimal results.
- && and || short-circuit; & and | always evaluate both sides.
- Compound assignment operators (+=, -=, etc.) include an implicit narrowing cast.
- The ternary operator is a compact substitute for a simple if-else that returns a value.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering