Variables and Operators
by Gaurav Gupta
- Declaring and assigning variables
- Arithmetic operators
- Relational operators
- Logical operators
- Operator precedence
- Shorthand assignment operators
- Exercises
- More practice
A variable is a named storage location that holds a value of a specific type. Operators combine values (and variables) into expressions that evaluate to a new value.
Declaring and assigning variables
1
type name = value;
Example:
1
2
3
4
5
int age = 19;
double price = 4.95;
char grade = 'D';
boolean isEnrolled = true;
String unit = "COMP1010";
The most commonly used types are:
| Type | Holds | Example literal |
|---|---|---|
int |
whole numbers | 42, -7 |
double |
floating-point numbers | 3.14, 17.0 |
char |
a single character | 'a', '7' |
boolean |
true or false |
true |
String |
a sequence of characters | "hello" |
Note that double is the preferred floating-point type in Java (as opposed to float in Processing).
Naming rules
A variable name (identifier) -
- can contain letters, digits, underscore (
_) and dollar sign ($), - cannot start with a digit,
- cannot be a reserved word (such as
int,if,class), - is case-sensitive (
totalandTotalare different variables).
By convention, Java variable names use camelCase (numberOfStudents).
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
addition | 7 + 2 |
9 |
- |
subtraction | 7 - 2 |
5 |
* |
multiplication | 7 * 2 |
14 |
/ |
division | 7 / 2 |
3 |
% |
remainder (modulo) | 7 % 2 |
1 |
Integer division
If both operands are integers, / performs integer division and discards the fractional part.
1
2
3
4
int a = 17/5; // 3
double b = 17/5; // 3.0 (integer division happens FIRST, then 3 is stored as 3.0)
double c = 17/5.0; // 3.4 (one operand is a double, so the result is a double)
double d = (double)17/5; // 3.4 (casting 17 to a double before dividing)
Remainder operator
% gives the remainder after integer division. It is very useful for checking divisibility.
1
2
3
int r = 17 % 5; // 2
boolean isEven = (n % 2 == 0);
int lastDigit = n % 10;
Relational operators
Relational operators compare two values and produce a boolean.
| Operator | Meaning |
|---|---|
== |
equal to |
!= |
not equal to |
< |
less than |
<= |
less than or equal to |
> |
greater than |
>= |
greater than or equal to |
Do not confuse = (assignment) with == (comparison).
Logical operators
| Operator | Meaning | true when |
|---|---|---|
&& |
AND | both operands are true |
|| |
OR | at least one operand is true |
! |
NOT | the operand is false |
1
2
3
4
int x = 15;
boolean inRange = (x >= 10) && (x <= 20); // true
boolean outside = (x < 10) || (x > 20); // false
boolean notInRange = !inRange; // false
Operator precedence
From highest to lowest (operators on the same row are evaluated left to right):
()parentheses!, unary-, casting*,/,%+,-<,<=,>,>===,!=&&||=(assignment)
When in doubt, use parentheses - they make your intention clear to the reader too.
Shorthand assignment operators
1
2
3
4
5
6
7
x += 5; // x = x + 5;
x -= 5; // x = x - 5;
x *= 5; // x = x * 5;
x /= 5; // x = x / 5;
x %= 5; // x = x % 5;
x++; // x = x + 1;
x--; // x = x - 1;
Exercises
Questions marked with ^CORE make a significant impact on subsequent learning.
EXERCISE 1
Which one of the following is/are valid variable names?
chicken_LEGSCOMP10001stOneIn#hashtagwhy?chicken-LEGS
EXERCISE 2
What is the value of n when the following code executes?
1
int n = 17/5;
EXERCISE 3 ^CORE
What is the value of n when the following code executes?
1
double n = 17/5;
EXERCISE 4
What is the value of n when the following code executes?
1
double n = 17/5.0;
EXERCISE 5
What is the value of flag when the following code executes?
1
boolean flag = (5 > 3) && (6 == 2);
EXERCISE 6 ^CORE
What is the value of flag when the following code executes?
1
boolean flag = true || true && false;
EXERCISE 7
What are the values of a, b and c after the following code executes?
1
2
3
4
5
int a = 10;
int b = 3;
int c = a % b + a / b * 2;
a += c;
b = a - b;
EXERCISE 8
Write a boolean expression that is true if and only if the integer variable year holds a leap year. A year is a leap year if it is divisible by 4, except years divisible by 100, which are leap years only if they are also divisible by 400.