Liang Chapters 1-5: Elementary Programming, Selections, Math, Loops, Methods
A Java program is executed from the main method. Variables store data values. Every variable has a data type that determines the size and layout of its memory. (Liang, Ch. 2)
Java has eight primitive data types (Liang, Table 2.2):
| Type | Size | Range | Default |
|---|---|---|---|
byte | 8 bits | -128 to 127 | 0 |
short | 16 bits | -32,768 to 32,767 | 0 |
int | 32 bits | -2^31 to 2^31 - 1 | 0 |
long | 64 bits | -2^63 to 2^63 - 1 | 0L |
float | 32 bits | ~7 decimal digits | 0.0f |
double | 64 bits | ~15 decimal digits | 0.0 |
char | 16 bits | Unicode 0 to 65,535 | '\u0000' |
boolean | 1 bit | true or false | false |
Widening (implicit): byte → short → int → long → float → double. Narrowing (explicit): requires a cast operator. (Liang, Section 2.15)
A constant is declared using the final keyword. Once assigned, its value cannot be changed. (Liang, Section 2.6)
(int) 3.9?What is the output?
Selection statements let you choose which action to perform based on conditions. Java provides if, if-else, nested if, switch, and the conditional expression operator. (Liang, Ch. 3)
The switch statement matches a variable against a list of constant values. (Liang, Section 3.13)
The ternary operator condition ? expr1 : expr2 provides a shorthand for if-else. (Liang, Section 3.14)
From highest to lowest: ! → * / % → + - → < <= > >= → == != → && → || → = += -=
x after: int x = (5 > 3) ? 10 : 20;?break in a switch case?What is the output?
Java provides the Math class with many useful methods for performing mathematical operations. All methods in the Math class are static. (Liang, Ch. 4)
| Method | Description | Example |
|---|---|---|
Math.pow(a, b) | a raised to power b | Math.pow(2, 3) = 8.0 |
Math.sqrt(x) | Square root | Math.sqrt(16) = 4.0 |
Math.abs(x) | Absolute value | Math.abs(-5) = 5 |
Math.max(a, b) | Larger of two values | Math.max(3, 7) = 7 |
Math.min(a, b) | Smaller of two values | Math.min(3, 7) = 3 |
Math.random() | Random double in [0, 1) | (int)(Math.random() * 10) |
Math.ceil(x) | Round up | Math.ceil(2.1) = 3.0 |
Math.floor(x) | Round down | Math.floor(2.9) = 2.0 |
Math.round(x) | Round to nearest | Math.round(2.5) = 3 |
Math.ceil(-2.1)?What is the output?
Loops enable your program to execute statements repeatedly. Java provides three loop statements: while, do-while, and for. (Liang, Ch. 5)
The do-while loop executes the body at least once before checking the condition. (Liang, Section 5.3)
break immediately terminates the loop. continue skips the current iteration and moves to the next one. (Liang, Section 5.8)
for (int i = 1; i <= 10; i += 3)What is the output?
A method is a collection of statements grouped together to perform an operation. Methods help with code reuse and modular programming. (Liang, Ch. 6)
A method signature consists of the method name and parameter list. The return type is NOT part of the signature. (Liang, Section 6.4)
Overloading means defining multiple methods with the same name but different parameter lists. The compiler determines which method to call based on arguments. (Liang, Section 6.8)
A local variable is declared inside a method and is only accessible within that method. A variable declared in a for loop header is only accessible inside the loop. (Liang, Section 6.9)
What is the output?