© We Can Code IT, LLC
If variable a holds 10 and variable b holds 15, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
+ | addition | adds two operands | a + b |
25 |
- | subtraction | subtracts second operand from the first | a - b |
-5 |
* | multiplication | multiplies the operands | a * b |
150 |
/ | division | divides first operand by second operand | b / a |
1 |
% | modulus (remainder) | returns the remainder after integer division | b % a |
5 |
If variable a holds the value 42, then:
Operator | Operation | Description | Expression | Equivalent to… | a 's value afterwards |
---|---|---|---|---|---|
++ | increment | adds one to the value its int operand holds |
a++ |
a = a + 1 |
43 |
‐‐ | decrement | subtracts one from the value its int operand holds |
a-- |
a = a - 1 |
41 |
If variable a holds 10 and variable b holds 15, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
== | equal to | evaluates to true if the two operands are equal | a == 10 a == b |
true false |
!= | not equal to | evaluates to true if the two operands are not equal | a != 10 a != b |
false true |
If variable a holds 10 and variable b holds 15, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
> | greater than | evaluates to true of the first operand is greater than the second operand | a > b |
false |
>= | greater than or equal to | evaluates to true of the first operand is greater than or equal to the second operand | a >= b a >= 10 |
false true |
< | less than | evaluates to true of the first operand is less than the second operand | a < b |
true |
<= | less than or equal to | evaluates to true of the first operand is less than or equal to the second operand | a <= b a <= 10 |
true true |
If variable a holds true
and variable b holds false
, then:
Operator | Operation | Description | Expression | Result |
---|---|---|---|---|
&& | conditional AND | evaluates to true if both operands are true; otherwise, evaluates to false |
a && b a && true |
false true |
|| | conditional OR | evaluates to true if either operand is true; otherwise, evaluates to false |
a || b b || false |
true false |
Let's translate English statements into Java. First, we'll do one together.
Start by writing the statements as comments. Here's an example:
// Jessica is 23 years old.
int jessicaAge = 23;
// Sam is 47.
int samAge = 47;
// Jessica is younger than Sam.
System.out.println(jessicaAge < samAge);
We need to create a variable to hold the age of Jessica. What should its type be?
// Jessica is 23 years old.
int
:
int jessicaAge
The word "is" means equals. How do we represent assignment?
// Jessica is 23 years old.
int jessicaAge = 23;
To say Jessica is younger than Sam means we are comparing their ages. Which operator would we use to compare them?
// Jessica is younger than Sam.
System.out.println(jessicaAge < samAge);
Single line comments start with //
:
// Jessica is 23 years old.
int jessicaAge = 23;
// Sam is 47.
int samAge = 47;
// Jessica is younger than Sam.
System.out.println(jessicaAge < samAge);
Each statement ends with a semicolon. It acts like a period (.) in English at the end of a sentence.
// Jessica is 23 years old.
int jessicaAge = 23;
// Sam is 47.
int samAge = 47;
// Jessica is younger than Sam.
System.out.println(jessicaAge < samAge);
The statement:
int jessicaAge = 23;
reads as "Jessica's age is 23."
println
is a method, so it ends with a set of opening and closing parentheses:
System.out.println(jessicaAge < samAge);
println
method, this is what we want to print. In this case, that's whether Jessica is younger than Sam:
System.out.println(jessicaAge < samAge);