0
0
Javaprogramming~10 mins

Writing first Java program - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writing first Java program
Start program
Define class
Define main method
Execute System.out.println
Print message to console
Program ends
The program starts by defining a class, then the main method runs, printing a message to the console, and then ends.
Execution Sample
Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
This program prints 'Hello, world!' to the console.
Execution Table
StepActionCode LineOutput
1Start program executionpublic class HelloWorld {
2Enter main methodpublic static void main(String[] args) {
3Execute print statementSystem.out.println("Hello, world!");Hello, world!
4End main method}
5End class}
💡 Program ends after printing the message.
Variable Tracker
VariableStartAfter Step 3Final
argsnull (not used)null (not used)null (not used)
Key Moments - 3 Insights
Why do we need the main method?
The main method is the starting point of any Java program. The execution_table row 2 shows entering the main method where the program begins running.
What does System.out.println do?
It prints the text inside the parentheses to the console. Execution_table row 3 shows this action printing 'Hello, world!'.
Why is the class needed?
Java programs must be inside a class. The class groups the code. Execution_table rows 1 and 5 show the start and end of the class.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 3?
A"Hello"
B"Hello, world!"
CNothing
D"world!"
💡 Hint
Check the Output column at step 3 in the execution_table.
At which step does the program start running the main method?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the Action column to find when main method execution begins.
If we remove the main method, what happens?
AProgram does not run (no entry point)
BProgram compiles but does nothing
CProgram runs and prints message
DProgram prints an error message
💡 Hint
Recall key_moments about the role of the main method.
Concept Snapshot
Java programs start with a class.
The main method is the entry point: public static void main(String[] args).
Use System.out.println("text") to print to console.
Program runs from main method and ends after it finishes.
Every Java program needs a main method to run.
Full Transcript
This Java program starts by defining a class named HelloWorld. Inside the class, the main method is defined, which is the starting point of the program. When the program runs, it enters the main method and executes the System.out.println statement, which prints 'Hello, world!' to the console. After printing, the program ends. The main method is essential because Java looks for it to start running the program. The class groups the code together. The variable args is present but not used in this example.