Java Virtual Machine, compilation, execution, and control flow
.java are compiled into .class.class files are interpreted by the JVM into native OS operations.class filesout/ folderjavac (Java compiler):
javac Identity.java
main method, or press Shift+F10main methodsjava command:
java Identity
static main methodmain and press Tab in IntelliJ - it will auto-complete!package fr.tbr.exercises;
public class JavaSyntaxDemo {
private String demoVersion = "1.0_DEV";
//Constructor
public JavaSyntaxDemo() {
}
public String getDemoVersion(){
return this.demoVersion;
}
public static void main(String[] args){
System.out.println("=> Begin execution");
JavaSyntaxDemo demo = new JavaSyntaxDemo();
System.out.println("Working with version : " + demo.getDemoVersion());
System.out.println("<= Exit");
}
}
in and outScanner scanner = new Scanner(System.in);
try { //this is a try-catch block, we will discuss it further
int num1 = scanner.nextInt();
System.out.println("Input 1 accepted");
int num2 = scanner.nextInt();
System.out.println("Input 2 accepted");
} catch (InputMismatchException e) {
System.out.println("Invalid Entry");
}
System.out.println("Say something in the console");
if(...){ }else{ } statementif parenthesis, must be a boolean// A boolean to do the demo
final boolean tester = true;
final boolean anotherTester = false;
/* The if instruction is followed by a parenthesis block, allowing to
* put the condition. If the condition is matched, then the curly
* bracket is executed. Else the "else" block is performed */
if (tester) {
// do something
} else {
// do something else
}
switch - case statement
int toBeTested = 2;
// Switches can be based on integer values, chars, or enums. Since java
// 7, it can even be based on Strings
switch (toBeTested) {
case 1:
// treat case one;
break; // put this break so the case stops
case 2:
// in this example, this the case we pass into
break;
default:
// this is the instruction to perform if no cases were satisfied
break;
}
An enum (enumeration) defines a fixed set of named constants:
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public enum AccountType {
SAVINGS, CHECKING, INVESTMENT
}
// Usage
Day today = Day.MONDAY;
AccountType type = AccountType.SAVINGS;
Enums work perfectly with switch statements:
public enum TrafficLight {
RED, YELLOW, GREEN
}
public void handleLight(TrafficLight light) {
switch (light) {
case RED:
System.out.println("Stop!");
break;
case YELLOW:
System.out.println("Caution - prepare to stop");
break;
case GREEN:
System.out.println("Go!");
break;
}
}
Modern Java introduced switch expressions with arrow syntax:
String result;
switch (day) {
case MONDAY:
case FRIDAY:
result = "Work";
break;
case SATURDAY:
case SUNDAY:
result = "Weekend";
break;
default:
result = "Midweek";
}
String result = switch (day) {
case MONDAY, FRIDAY -> "Work";
case SATURDAY, SUNDAY -> "Weekend";
default -> "Midweek";
};
break neededyieldFor complex logic in a case, use yield to return a value:
int numLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> {
// Complex logic goes here
System.out.println("It's Wednesday!");
yield 9; // yield returns the value
}
};
-> for single expressions (no yield needed){ yield value; } for blocks with multiple statementsSwitch can now match on types, not just values:
// Type pattern matching in switch
String describe(Object obj) {
return switch (obj) {
case Integer i -> "Integer: " + i;
case String s -> "String of length " + s.length();
case Double d -> "Double: " + d;
case null -> "It's null!";
default -> "Unknown type";
};
}
// With guards (when clause)
String categorize(Integer num) {
return switch (num) {
case Integer n when n < 0 -> "Negative";
case Integer n when n == 0 -> "Zero";
case Integer n when n > 0 -> "Positive";
default -> "Unknown";
};
}
| Feature | Traditional | Expression |
|---|---|---|
| Returns value | No (use variable) | Yes (directly) |
| Break statement | Required | Not needed |
| Multiple cases | Fall-through | Comma-separated |
| Exhaustiveness | Not enforced | Must cover all cases |
| Pattern matching | Not supported | Supported (Java 21) |
Enums can have fields, constructors, and methods:
public enum AccountType {
SAVINGS(0.02), // 2% interest
CHECKING(0.0), // 0% interest
INVESTMENT(0.05); // 5% interest
private final double interestRate;
AccountType(double rate) {
this.interestRate = rate;
}
public double getInterestRate() {
return interestRate;
}
}
// Usage
AccountType type = AccountType.SAVINGS;
double rate = type.getInterestRate(); // 0.02
Debugging is the process of finding and fixing errors in your code.
System.out.println() statements
A breakpoint tells the debugger to pause execution at a specific line:
public void calculateInterest(double balance) {
double rate = 0.05;
double interest = balance * rate; // <-- Set breakpoint here
System.out.println("Interest: " + interest);
}
When execution reaches the breakpoint, the program pauses and you can inspect values.
Once paused at a breakpoint, use these controls:
| Action | Shortcut | Description |
|---|---|---|
| Step Over | F8 | Execute current line, move to next |
| Step Into | F7 | Enter the method being called |
| Step Out | Shift+F8 | Finish current method, return to caller |
| Resume | F9 | Continue until next breakpoint |
Variables panel: Shows all variable values at current point
The following code has a bug. Use the debugger to find it:
public class BuggyCalculator {
public static double calculateAverage(int[] numbers) {
int sum = 0;
for (int i = 0; i <= numbers.length; i++) { // Bug here!
sum += numbers[i];
}
return sum / numbers.length;
}
public static void main(String[] args) {
int[] grades = {85, 90, 78, 92, 88};
double avg = calculateAverage(grades);
System.out.println("Average: " + avg);
}
}
ii reaches 5?