Loading
0x00Lesson 1 of 6

Run your first Java program

Meet the main method and print a message from a Java class.

14 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Recognize a Java class and print a line of output.

Java programs are organized into classes. A program starts at the main method, whose familiar signature is public static void main(String[] args). System.out.println writes a line to standard output. Java source is compiled to bytecode and run by a Java Virtual Machine (JVM).

Main.java
1class Main {
2    public static void main(String[] args) {
3        System.out.println("Hello, Java!");
4    }
5}
Output
Hello, Java!

Statements end with semicolons, and braces group class and method bodies. These exercises use a package-private Main class because the online compiler gives source files a generic name; in a local project, a public class name normally matches its .java filename.

Key takeaways

  • Recognize a Java class and print a line of output.

  • Java checks types as it compiles your source.

  • Use compiler messages as clues about what needs fixing.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Java

Write Java in the editor and run it against sample input with a remote compiler. Your code is sent to Compiler Explorer to compile and run.

Exercise 1

Print a greeting

+25 XP

Print exactly Hello, Java! on one line from the main method.

  • Greeting
Main.java
Loading editor…

Java is compiled and run by Compiler Explorer (godbolt.org), a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: