Bird
Raised Fist0
Javaprogramming~10 mins

Writing first Java program - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of the main method in a Java program?
easy
A. It is the starting point where the program begins execution.
B. It defines a new class in Java.
C. It is used to declare variables.
D. It imports external libraries.

Solution

  1. Step 1: Understand the role of the main method

    The main method is special because Java looks for it to start running the program.
  2. Step 2: Identify the program entry point

    Without the main method, the program has no defined starting point to execute code.
  3. Final Answer:

    It is the starting point where the program begins execution. -> Option A
  4. Quick Check:

    Main method = program start [OK]
Hint: Remember: Java runs code starting from main method [OK]
Common Mistakes:
  • Thinking main defines a class
  • Confusing main with variable declaration
  • Assuming main imports libraries
2. Which of the following is the correct syntax to print "Hello, World!" with a newline in Java?
easy
A. System.out.print("Hello, World!");
B. print("Hello, World!");
C. System.out.println("Hello, World!");
D. echo "Hello, World!";

Solution

  1. Step 1: Identify the correct print method in Java

    Java uses System.out.println() to print text with a new line.
  2. Step 2: Check each option for syntax correctness

    System.out.print("Hello, World!"); uses print which prints without a new line, but is valid. System.out.println("Hello, World!"); uses println which prints with a new line and is the common way. print("Hello, World!"); and D are not valid Java syntax.
  3. Final Answer:

    System.out.println("Hello, World!"); -> Option C
  4. Quick Check:

    Print line = System.out.println() [OK]
Hint: Use System.out.println() to print with a new line [OK]
Common Mistakes:
  • Using print without System.out
  • Using non-Java print commands
  • Missing quotes around text
3. What will be the output of the following Java program?
public class Hello {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
    }
}
medium
A. Syntax Error
B. Hello
C. Welcome to Java!\n
D. Welcome to Java!

Solution

  1. Step 1: Read the print statement inside main

    The program prints the exact string "Welcome to Java!" using System.out.println.
  2. Step 2: Understand println adds a new line but does not show \n literally

    The output shows the text and moves to the next line, but the new line is not visible as characters.
  3. Final Answer:

    Welcome to Java! -> Option D
  4. Quick Check:

    Print output = Welcome to Java! [OK]
Hint: System.out.println prints text exactly as given [OK]
Common Mistakes:
  • Confusing output with class name
  • Expecting \n characters in output
  • Assuming syntax error without reason
4. Identify the error in the following Java program:
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello Java")
    }
}
medium
A. Class name should be lowercase.
B. Missing semicolon after the print statement.
C. main method should not have String[] args.
D. System.out.println should be System.print.

Solution

  1. Step 1: Check syntax of print statement

    The print statement is missing a semicolon at the end, which is required in Java.
  2. Step 2: Verify other parts for correctness

    Class name capitalization is allowed, main method signature is correct, and System.out.println is the right method.
  3. Final Answer:

    Missing semicolon after the print statement. -> Option B
  4. Quick Check:

    Semicolon missing = syntax error [OK]
Hint: Always end statements with a semicolon in Java [OK]
Common Mistakes:
  • Omitting semicolon at line end
  • Changing class name case unnecessarily
  • Incorrect main method signature
5. You want to create a Java program that prints "Good Morning!". Which of the following is the correct file name and class name combination to run the program successfully?
hard
A. File name: GoodMorning.java, Class name: GoodMorning
B. File name: goodmorning.java, Class name: GoodMorning
C. File name: GoodMorning.java, Class name: goodmorning
D. File name: GoodMorning.txt, Class name: GoodMorning

Solution

  1. Step 1: Understand Java file and class naming rules

    The file name must exactly match the public class name including case, with .java extension.
  2. Step 2: Check each option for matching names and extension

    File name: GoodMorning.java, Class name: GoodMorning matches file and class names exactly with correct extension. Options B and C have case mismatches. File name: GoodMorning.txt, Class name: GoodMorning has wrong file extension.
  3. Final Answer:

    File name: GoodMorning.java, Class name: GoodMorning -> Option A
  4. Quick Check:

    File name = Class name + .java [OK]
Hint: Match file name exactly with class name and use .java extension [OK]
Common Mistakes:
  • Mismatching class and file names
  • Using wrong file extension
  • Ignoring case sensitivity
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard