Skip to content
Ameen Abozaid
BEG-008

First Java program

  • 6 Lessons
  • 70 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Java syntax
  • main()
  • classes
  • statements
  • comments
  • console output

Learn first

70 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Identify the parts of a minimal Java program: class declaration, main method signature, statements, braces.
  2. Print text and simple values with System.out.println and System.out.print.
  3. Write single-line, multi-line, and Javadoc comments, and explain when a comment is useful.
  4. Fix the five most common beginner syntax errors from the compiler message alone.

Lessons

  1. 01Anatomy of Hello World12 min read
  2. 02Statements, semicolons, and blocks11 min read
  3. 03Printing output11 min read
  4. 04Comments12 min read
  5. 05Reading compiler errors12 min read
  6. 06Demo: fixing five broken programs from compiler messages12 min read

Lab and exercises

Lab

BEG-008-LAB1 · Business card

Outcomes: 1–4 · Time: ~2 h

  1. Print a formatted multi-line business card (name, role, email, phone) using println and print.
  2. Add a Javadoc comment on the class and one useful // comment.
  3. Fix the five broken programs below using only the compiler messages.

Acceptance: all five compile and print Hello; the card prints as designed.

The five broken programs (all should print Hello)
java
// B1
public class B1 {
    public static void main(String[] args) {
        System.out.println("Hello")
    }
}
java
// B2
public class B2 {
    public static void main(String[] args) {
        system.out.println("Hello");
    }
}
java
// B3
public class B3 {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
java
// B4
public class B4 {
    public static void main(String args) {
        System.out.println("Hello");
    }
}
java
// B5
public class B5 {
    public static void main(String[] args) {
        System.out.println("Hello);
    }
}

Exercises (assessment: EX01–EX05)

IDOutcomeTask
BEG-008-EX011Label the parts of Hello World: class line, main line, braces, statement.
BEG-008-EX022Print your name on one line and your city on the next, using only print.
BEG-008-EX032Print Total: 3 items by joining text and the number 3.
BEG-008-EX043Add a single-line, a multi-line, and a Javadoc comment to any program. Which one would you keep and why?
BEG-008-EX054Rename main to Main and read the error. What does the JVM say?
Show solutions
  • Lab fixes B1 add ;. B2 System. B3 add the final }. B4 String[] args. B5 close the quote.
  • EX01 public class Hello is the class line; public static void main(String[] args) is the entry point; { } are blocks; System.out.println(...); is the statement.
  • EX02 System.out.print("Ana\n"); System.out.print("Cairo");
  • EX03 System.out.println("Total: " + 3 + " items");
  • EX04 Keep comments that explain why, not what.
  • EX05 Compile succeeds but running reports that no main method was found.

Quiz

Outcomes: O1 program parts · O2 printing · O3 comments · O4 compiler errors.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Where does a Java program start?O1
  2. 2The correct main line is:O1
  3. 3Hello.java must contain:O1
  4. 4Java is:O1
  5. 5System.out.println("A"); System.out.println("B"); prints:O2
  6. 6"Age: " + 30 produces:O2
  7. 7Which is a Javadoc comment?O3
  8. 8A good comment explains:O3
  9. 9';' expected usually means:O4
  10. 10For system.out.println("Hi"); the compiler reports package system does not exist. The most likely cause is:O4

Practical part

Assessment is BEG-008-EX01 to EX05 plus the five broken programs. Pass: at least four of five programs fixed.

Common mistakes

Case sensitivity (system.out), missing semicolons, mismatched braces, misspelling main's signature.

Assessment

5 exercises (BEG-008-EX01..05).

Career relevance

Reading compiler and tool output is a daily skill at every level.

Type to search levels, phases, modules and lessons.