Java Notes
Operators are symbols that tell Java to calculate, compare, combine conditions, update a value, or choose between two results.
An operator is a symbol that tells Java to do something with values. You already know some operators from maths: + adds and - subtracts. Java also has operators for checking a condition, joining conditions, and updating a value.
For example, in total + 5, the operator is +. In score >= 40, the operator is >=. The first calculation makes a number; the second calculation answers a yes/no question.
Arithmetic operators: calculate with numbers
Arithmetic operators work with numeric values.
| Symbol | What it does | Example with 17 and 5 |
|---|---|---|
+ | Add | 17 + 5 gives 22 |
- | Subtract | 17 - 5 gives 12 |
* | Multiply | 17 * 5 gives 85 |
/ | Divide | 17 / 5 gives 3 with int values |
% | Give the remainder | 17 % 5 gives 2 |
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
notebookPrice * quantity finds the total cost. paidAmount - totalCost finds the change. The last line uses %: when a number divided by 2 leaves remainder 0, the number is even.
Division with whole numbers
When both values are int, Java keeps only the whole part of a division. 17 / 5 becomes 3, not 3.4. Use a decimal type or cast one number when you need the exact decimal answer.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The cast changes total into a decimal before Java divides. See the Type Casting lesson for the full explanation.
Comparison operators: ask a yes/no question
Comparison operators compare two values. Their result is always a boolean: either true or false.
| Symbol | Question it asks | Example |
|---|---|---|
== | Are these equal? | score == 40 |
!= | Are these different? | score != 40 |
> | Is left bigger? | score > 40 |
< | Is left smaller? | score < 40 |
>= | Is left bigger or equal? | score >= 40 |
<= | Is left smaller or equal? | score <= 100 |
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The brackets are important when joining text and a comparison. "Passed: " + (score >= 40) first checks the condition, then joins the word true or false to the label.
Do not confuse = with ==. One equals sign stores a value: score = 68;. Two equals signs compares values: score == 68.
Logical operators: join conditions
Sometimes one question is not enough. Logical operators combine true/false answers.
| Symbol | Meaning | Example question | ||
|---|---|---|---|---|
&& | AND: both answers must be true | Is the learner logged in and enrolled? | ||
| ` | ` | OR: at least one answer must be true | Is it a weekend or a holiday? | |
! | NOT: reverse the answer | Is the learner not logged in? |
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
canStudy is false because both conditions are required and hasCourseAccess is false. canManageCourse is true because an admin is allowed even without ordinary course access. !isLoggedIn reverses true to false.
Why && can prevent an error
Java checks the left side of && first. If it is false, Java already knows the whole AND condition is false and does not need to check the right side.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
First Java checks whether courseName has text. It does not, so Java stops the condition there and does not try courseName.length(). This avoids trying to count letters in missing text.
Assignment operators: store and update values
= gives a variable a value. Short forms such as += update a value using the value already stored there.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
The first update reads 4, adds 1, and stores 5. completedLessons += 2 is a shorter way to write completedLessons = completedLessons + 2. Use -=, *=, and /= in the same way when they make an update clearer.
Conditional operator: choose one of two values
The conditional operator ?: is a short form for a simple if-else decision. It has three parts:
condition ? valueWhenTrue : valueWhenFalse
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java first checks score >= 40. Because it is true, it stores "Pass" in result. If the score were below 40, it would store "Fail". Use ?: only for a short, clear choice. Use if-else when each choice needs several lines of work.
Common operator mistakes
Confusing assignment with comparison
= puts a value into a variable. == asks whether two values are equal. Using the wrong one can change a value when you meant to ask a question.
int attempts = 3;
// if (attempts = 0) { } // Wrong: this tries to assign 0.
if (attempts == 0) {
System.out.println("No attempts left");
}Forgetting that integer division drops decimals
If both sides of / are int, Java gives a whole-number result. Cast one side before dividing when the decimal matters.
Using & when you mean &&
&& is the logical AND operator and can stop early when the first condition is false. A single & does not provide that short-circuit behaviour, so use && for ordinary conditions.
Try it yourself
Create a class named TicketCheck. Store an age and a boolean named hasTicket. Print whether the person may enter: they must be at least 18 and have a ticket.
Hint
Make a boolean variable called canEnter. Use age >= 18 && hasTicket on the right side of =.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Change the age and ticket value one at a time. Run the program after each change and observe why && requires both answers to be true.
Continue the lesson path