Command Palette

Search for a command to run...

Back to the lesson: Topic 0.4 — Install the JDK and Run Your First Program
Core Java · Example 2 of 3

Your first program, a little bigger

print stays on the same line; println ends the line. An empty println() prints a blank line.

Install a free JDK (Eclipse Temurin), check it with java -version and javac -version, then write Main.java, compile it with javac Main.java and run it with java Main. Since Java 11 you can also skip the separate compile step and run java Main.java directly.

Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a program that prints a three-line greeting: your name, your favourite food, and your favourite colour.

02

Write a program that prints the word Java on one line using four separate System.out.print calls, one letter each, then ends the line.

03

Write a program that draws a small triangle of stars, three lines high (*, **, ***).

Explain it without notes

01

What is the difference between javac Main.java, java Main and java Main.java?

02

What are PATH and JAVA_HOME, and why does Java need them?

03

Why must the file holding public class Main be named Main.java?

04

You ran java -version and got 17, but you installed 21. What is going on and how do you fix it?

Your first program, a little bigger
Sign in to run this example in your browser.

Expected output

Hello! My name is Asha.
I am learning Java today.

2 + 3 = 5
Have a great day!