Java
Errajae Abdellah
Concept Summaries
1. User Input
Use the Scanner class to read input from the user.
import [Link];
Scanner scanner = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Hello, " + name + "!");
2. Arithmetic & Math Class
Operators like +, -, *, /, % are used for calculations.
The Math class adds advanced functions like [Link](), [Link](), [Link](), etc.
int x = 10, y = 3;
double result = [Link](x, y); // 10³ = 1000
3. If Statements & Nested If
Control flow — run code only if a condition is true.
if (score >= 90) {
[Link]("A");
} else if (score >= 80) {
[Link]("B");
} else {
[Link]("C");
Nested ifs let you check multiple conditions inside others.
4. Logical Operators
Used to combine conditions:
• && (AND)
• || (OR)
• ! (NOT)
if (age >= 18 && age <= 25) {
[Link]("You can enter.");
}
5. Switch & Enhanced Switch
Switches choose between multiple options — enhanced ones are more compact.
int day = 3;
switch (day) {
case 1 -> [Link]("Monday");
case 2 -> [Link]("Tuesday");
default -> [Link]("Other day");
6. Ternary Operator
Short form of if-else:
String result = (age >= 18) ? "Adult" : "Minor";
7. Loops
While loop
Repeats code while a condition is true.
while (count < 5) {
[Link](count);
count++;
}
For loop
Used when you know how many times to loop.
for (int i = 0; i < 5; i++) {
[Link](i);
Nested loops
A loop inside another (e.g., for printing patterns or matrices).
8. Break & Continue
• break → exits a loop
• continue → skips to next iteration
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
[Link](i);
9. Methods
Reusable blocks of code.
static void greet(String name) {
[Link]("Hello, " + name);}
greet("Alice");
10. Strings & Substrings
Strings are text values; have many useful methods:
String s = "Java";
[Link]([Link]()); // 4
[Link]([Link]()); // JAVA
[Link]([Link](1,3)); // av
11. printf()
Used for formatted output (like C’s printf).
[Link]("You have $%.2f left%n", 12.3456);
12. Random Numbers
Use Random or [Link]():
import [Link];
Random rand = new Random();
int randomNum = [Link](10); // 0–9