Object-Oriented Programming through Java · Language Basics
Operators
In Object-Oriented Programming through Java because a ticket price is computed — arithmetic, comparisons, bits, a ternary choice, and ++ are how values become new values.
An operator takes values and produces a new value. Meera's student fare is 200 minus 50; whether she pays that fare is a boolean; a flag packed in bits is a different kind of and. This lesson keeps those operators apart and shows the ++ trap.
- Object-Oriented Programming through Java
- Easy level
- 6 concepts
1An operator produces a new value
An operator takes one or two values and produces a new value. 200 - 50 is the value 150. It does not change 200 unless you store the result back into a box.
The name on the left of = is the box you write into. The right-hand side is the value you computed.
Figure. 200 - 50 is the value 150. It does not change 200 unless you store the result. Assignment writes that new value into paid.
How it works
- An operator produces a new valueOperators make a new value. Assignment is what writes that value into a box.
Compute a student fare; leave listPrice alone
int listPrice = 200;
int paid = listPrice - 50;
System.out.println(paid);2Arithmetic, integer division, and order
+ - * / % work on numbers. Integer / drops the fraction: 5 / 2 is 2, not 2.5. % is the remainder after that kind of division.
Precedence: * / % before + -. Same rank runs left to right. Parentheses first. Write parentheses when a reader would have to hesitate.
No diagram — the idea is carried by the prose or the Java listing.
| Operator | Meaning | On 200 and 50 |
|---|---|---|
| + | Add | 250 |
| - | Subtract | 150 |
| * | Multiply | 10000 |
| / | Divide (int) | 4 |
| % | Remainder | 0 |
Order changes the fare
Compute 2 + 3 * 4, then (2 + 3) * 4, then the integer division 5 / 2.
- 3 * 412
- 2 + 1214
- (2 + 3) * 420
- 5 / 2 (int)2
Pro tip. 5 / 2.0 is 2.5 because one side is already a double. 5 / 2 stays in ints.
Coding lab. Compute the student fare runs in the app, with checks on your output.
3++ after the name yields the old value
++ adds one. -- subtracts one. Written before the name (++x) it changes first, and the value of the expression is the new one.
Written after (x++) the expression's value is the old one, then it changes. The trap is using x++ inside a larger expression and expecting the new value.
Figure. seatsLeft++ yields the old 5 into shown, then seatsLeft becomes 6. The trap is expecting the new value from x++ inside a larger expression.
shown is 5; seatsLeft becomes 6
int seatsLeft = 5;
int shown = seatsLeft++;
System.out.println(shown);
System.out.println(seatsLeft);Prefix versus postfix on a seat counter
Start with x = 5. What are y and x after y = x++, and after a fresh y = ++x?
- start x5
- y = x++ (y, then x)5, then 6
- reset x5
- y = ++x (y, then x)6, then 6
Pro tip. If you only want to add one and do not use the value of the ++ expression, write x = x + 1 or x++ on its own line.
4Comparisons and short-circuit logic
Relational operators produce boolean: < <= > >= == !=. On primitives, == compares values.
&& is and, || is or, ! is not. && and || stop early: if the left of && is false, the right is not evaluated. That matters when the right side would fail — for example reading args[0] when args is empty.
No diagram — the idea is carried by the prose or the Java listing.
| Write | True when |
|---|---|
| price >= 200 | Price is 200 or more |
| student && price >= 200 | Both sides true; right skipped if student is false |
| student || staff | At least one is true |
| !student | student is false |
A boolean from two tests
boolean student = true;
int price = 200;
boolean cheapSeat = student && price >= 200;5Bits are not boolean &&
Bitwise operators work on the bits of integers: & and, | or, ^ xor, ~ not, << and >> shifts.
5 & 3 is 1 because 101 and 011 share only the last 1. These are not the same as && and ||. Do not use & when you mean both conditions.
No diagram — the idea is carried by the prose or the Java listing.
| Write | Works on | Use when |
|---|---|---|
| && || ! | booleans | Conditions |
| & | ^ ~ | integer bits | Flags, masks |
Bitwise and on two small ints
What is 5 & 3, written in bits then as a decimal?
- 5 in bits101
- 3 in bits011
- 101 & 011001
- 001 as decimal1
Pro tip. 5 && 3 does not compile: && wants booleans. 5 & 3 is the bit pattern.
6A short choice between two values
condition ? ifTrue : ifFalse picks one of two values. It is an expression, so it can sit on the right of =.
Use it for a short choice. An if/else is clearer when each branch is more than one value, or when you need statements rather than a value.
Figure. student ? 150 : 200 picks one of two values. It is an expression, so it can sit on the right of =. Here student is true, so paid is 150.
Student fare in one expression
boolean student = true;
int paid = student ? 150 : 200;
System.out.println(paid);Student flag picks the fare
paid = student ? 150 : 200. What is paid when student is true, then when student is false?
- student is truecondition holds
- pick the ifTrue value150
- student is falsecondition fails
- pick the ifFalse value200
Pro tip. The ternary is an expression: it yields 150 or 200. It does not run a block of statements.
Notes
- In Object-Oriented Programming through Java because a ticket price is computed — arithmetic, comparisons, bits, a ternary choice, and ++ are how values become new values.
- Integer division drops the fraction: 5 / 2 is 2.
- && is a boolean and. & on integers is a bitwise and. They are not interchangeable.
Exam traps & shortcuts
- Write parentheses when a reader would have to hesitate about order.
- x++ in a larger expression yields the old x. ++x yields the new one.
Recap
Operators make new values, integer division cuts, x++ yields the old x, and & is not &&.
- An operator produces a new value
- 200 - 50 is 150. Assignment is what writes that into a box.
- Arithmetic and order
- 5 / 2 is 2. Parentheses first; * / % before + -.
- ++ after the name
- x++ yields the old x, then changes x.
- Bits are not boolean &&
- 5 & 3 is 1. Do not use & when you mean both conditions.
Practise Operators
Reading is free and needs no account. Practice, mocks and progress live in the app.
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device