Based on chapter 2 of the textbook "Building Java Programs", 3rd edition. Covers primitive data types, variables, operators, ASCII values for chars, operator precedence, String concatenation, casting, for loops, nested for loops, and class constants.
See a video presentation of this slideshow on my YouTube channel JavaGoddess, at https://www.youtube.com/watch?v=N7SBkMY65gc&t=4s
4. Data Types
Type Representation
int Integers from negative to positive 2 -1
double Real numbers from negative to positive 10
char Single text characters in single quotes
boolean True/false values
String A line of text in double quotes
31
308
Primitive data types
Objects
7. Dividing with ints
4 / 2 is 2
But…
15 / 4 is not 3.75
You need to cut off what is after the decimal point.
15 / 4 is 3
You are NOT rounding--just leaving out digits.
But…
15.0 / 4 is 3.75
This is because 15.0 is a double, not an int.
8. Mod (modulus)
% finds the remainder after integer division
x % y is x - (x / y) * y
6 % 2 is 0
5 % 3 is 2
3 % 5 is 3
10 % 10 is 0
integer division
Mod Hacks
● If x % y == 0 is true, then x is
divisible by y.
● If x % 2 == 0, then x is even. If it is
1, then x is odd.
● x % 10 finds the last "a" number
of digits of x
● x / 10 % 10 finds the "a"th digit of
x starting from the left
a
a
9. double
Accurate to 16 places
Roundoff error examples (try them, they actually
work):
10.0 / 3.0 is 3.3333333333333335
3 * 3.2 is 9.600000000000001
10. char
The space bar ' ' is also valid
A single quote (') as a char is '''
11.
12. chars, ints, and ASCII Values
int a = ('a' + 'b') / 2; //becomes 97
/*
(97 + 98) / 2
195 / 2
97
*/
'a' - 98 cannot become a char
char c = 'c' - 2; //becomes 'a'
//99 - 2 is 97, which is 'a'
13. boolean
Symbol Meaning
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
14. Ands && Ors
True/False Table for && True/False Table for ||
true false
true true false
false false false
true false
true true true
false true false
Short-circuited evaluation:
1. If the first thing in an "and" statement is false, the entire "and" statement is
assumed to be false. 3 > 5 && 1 / 0 == 0 is assumed false.
2. If the first thing in an "or" statement is true, the entire "or" statement is
assumed to be true. 3 < 5 || 1 / 0 == 0 is assumed true.
16. Initializing Variables
The naming convention for variables isTheSameAsForMethods.
The equal sign means "to get the value of" or "becomes"
int b = 4;
int b = 2; //compile error
int c = 4;
int a = 5;
type
name
value
17. String Concatenation
"this " + "that" becomes "this that"
"This statement is " + true becomes "This statement is true"
"I am number " + 1 becomes "I am number 1"
"myVariable" ≠ myVariable
'6' ≠ 6
false ≠ "false"
4 + 2 + " is my number" becomes "6 is my number"
"" + 4 + 2 + " is my number" becomes "42 is my number"
"You're number " + 1 + 2 becomes "You're number 12"
"You're number " + (1 + 2) becomes "You're number 3"
20. Operator Precedence
PrecedenceRank Operator Category Example
1 Parentheses ( )
2 Unary minus sign in front of numbers
3 Casting (int), (char), (double), 5 → 5.0, 'a' → 97
4 Multiplicative *, /, %
5 Additive + for adding, -, + for concatenation
6 Relational <, <=, >, >=
7 Equality ==, !=
8 And &&
9 Or ||
10 Assignment =
21. Operation Shorthands
Expression Shorthand
x = x + 3; x += 3;
x = x - 4; x -= 4;
x = x * 2; x *= 2;
x = x / 5; x /= 5;
x = x + 1; //incrementing x++; x += 1;
x = x - 1; //decrementing x--; x -= 1;
23. What is a For Loop?
for (int i = 1; i <= 5; i++) {
System.out.print(i);
}
Initialization
(statement)
Test
(boolean
variable)
Update
(statement)
Statement(s)
for (;;) {
System.out.println("Infinite loop!");
}
24. How a For Loop Runs
for (int i = 1; i <= 5; i++) {
System.out.print(i);
}
Do the
initialization
Is the test
true?
Yes
No
Exit for loop
Do the statements inside
Do the update
25. Nested For Loops
for (int i = 1; i <= 50; i++) {
for (int j = 1; j <= 50; j++) {
System.out.print("*");
} //prints out one line
System.out.println();
} //repeats the line
i j
1 1
2
3
2 1
2
3
3 1
2
3
26. Class Constants
public class ClassConstant {
public static final int SIZE = 20;
//final means it cannot be changed after declaring
public static void main(String[] args) {
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Class constant naming convention:
ALL_CAPS_WITH_UNDERSCORES