CodeBlanks Logo Icon CodeBlanks

Summary - Java basics

Combine main, comments and the class skeleton to write your first Java program.

Summary - Java basics

In this group you learned the basic building blocks of Java:

  • Java: compiled, object-oriented, platform-independent language
  • main method: the entry point of a program
  • Comments: explanation for humans with // and /* */
  • Class skeleton: the public class Main shell
  • javac/java: compile and run commands

Now combine all of these and write a working program yourself.

Practice Write a two-line program

Define a Main class with a main method. From main, print Hello first and Java 21 second with two println calls.

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello");
        System.out.println("Java 21");
    }
}

Expected output

Hello
Java 21

Practice Add a comment and output

Write a Main class, a main method and two println calls. Add a // Opening comment above the first println.

Starter code

Main.java
public class Main {

}

Sample solution

Main.java
public class Main {
    public static void main(String[] args) {
        // Opening
        System.out.println("Opening");
        System.out.println("Welcome");
    }
}

Expected output

Opening
Welcome

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the concepts

Term Meaning
public class Class shell
main Entry point
// Comment
javac Compiler

2 Complete the class skeleton

Question
public class Main { public static ____(String[] args) { } }
Answer
public class Main { public static void(String[] args) { } }

Explanation: main returns no value.

All lessons in Beginner