First Java program
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
- Identify the parts of a minimal Java program: class declaration,
mainmethod signature, statements, braces. - Print text and simple values with
System.out.printlnandSystem.out.print. - Write single-line, multi-line, and Javadoc comments, and explain when a comment is useful.
- Fix the five most common beginner syntax errors from the compiler message alone.
Lessons
Lab and exercises
Lab
BEG-008-LAB1 · Business card
Outcomes: 1–4 · Time: ~2 h
- Print a formatted multi-line business card (name, role, email, phone) using
printlnandprint. - Add a Javadoc comment on the class and one useful
//comment. - 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)
// B1
public class B1 {
public static void main(String[] args) {
System.out.println("Hello")
}
}// B2
public class B2 {
public static void main(String[] args) {
system.out.println("Hello");
}
}// B3
public class B3 {
public static void main(String[] args) {
System.out.println("Hello");
}// B4
public class B4 {
public static void main(String args) {
System.out.println("Hello");
}
}// B5
public class B5 {
public static void main(String[] args) {
System.out.println("Hello);
}
}Exercises (assessment: EX01–EX05)
| ID | Outcome | Task |
|---|---|---|
| BEG-008-EX01 | 1 | Label the parts of Hello World: class line, main line, braces, statement. |
| BEG-008-EX02 | 2 | Print your name on one line and your city on the next, using only print. |
| BEG-008-EX03 | 2 | Print Total: 3 items by joining text and the number 3. |
| BEG-008-EX04 | 3 | Add a single-line, a multi-line, and a Javadoc comment to any program. Which one would you keep and why? |
| BEG-008-EX05 | 4 | Rename main to Main and read the error. What does the JVM say? |
Show solutions
- Lab fixes B1 add
;. B2System. B3 add the final}. B4String[] args. B5 close the quote. - EX01
public class Hellois 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
mainmethod 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.
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.