Java Basics: Your First Programs
What Is Java? Your First Program
In this lesson you will
- Explain what Java and the JVM are
- Read the structure of a simple Java program
- Compile and run a program on your own computer
Introduction
Java is one of the most widely used programming languages. It runs banking systems, Android apps, huge websites and much of the software inside large companies. Its famous promise is "write once, run anywhere": the same Java program works on Windows, macOS and Linux.
How this course works
Unlike the other courses on this site, Java cannot run inside your web browser, so the examples here are read-only. Every program is shown with the output it produces. We ran all of them with a real Java 21 compiler, so the output is exactly what you will see on your own computer. To practise, you run the programs yourself, which the next sections explain.
How Java runs
You write Java in text files ending in .java. A Java program goes through two stages:
- The compiler (
javac) checks your code and translates it to bytecode. - The Java Virtual Machine (JVM) runs that bytecode.
The JVM is what makes "run anywhere" possible: every operating system has its own JVM, and they all understand the same bytecode. The toolkit with the compiler and tools is the JDK (Java Development Kit), which you download free from the Eclipse Temurin, Oracle or Microsoft websites.
Quick quiz
What does the Java compiler produce from your source code?
Your first program
Save this as Main.java:
Java
public class Main {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}When run, it prints:
Hello, world!Here is what each part means:
public class Maindeclares a class named Main. In Java, all code lives inside classes.public static void main(String[] args)is the entry point: the place where Java starts running your program. It must look exactly like this.System.out.println("Hello, world!");prints a line of text. Each statement ends with a semicolon;.- Curly brackets
{ }mark the start and end of blocks.
Java is case sensitive (Main and main are different) and cares about every semicolon and bracket. When something is missing, the compiler tells you.
Running it on your computer
Install a JDK (version 17 or newer). Open a terminal in the folder with your file and run:
Terminal
javac Main.java
java MainThe first command compiles Main.java into Main.class, and the second runs it. Since Java 11 there is also a shortcut that compiles and runs in one step, handy for small experiments:
Terminal
java Main.javaThe shortcut runs the first class in the file, so it suits a file with a single class. When a file has several classes, as later lessons do, use javac and java as above, which always work.
If you prefer, an editor such as VS Code, IntelliJ IDEA or Eclipse can run programs with a button.
More than one statement
Java
public class Main {
public static void main(String[] args) {
System.out.println("Line one");
System.out.println("Line two");
System.out.print("No new line, ");
System.out.print("so these join up");
System.out.println();
System.out.println("Done");
}
}Line one
Line two
No new line, so these join up
Doneprintln prints and moves to a new line. print does not. Statements run from top to bottom.
Comments
Comments are notes for humans that the compiler ignores.
Java
public class Main {
// a one-line comment
public static void main(String[] args) {
/* a comment
over several lines */
System.out.println("Comments are ignored"); // also at the end
}
}Comments are ignoredReading compiler errors
If you forget a semicolon, the compiler refuses to build the program, and points to the line:
Java
public class Main {
public static void main(String[] args) {
System.out.println("Oops")
}
}Main.java:3: error: ';' expected
System.out.println("Oops")
^
1 errorRead the message, look at the line number and the arrow, and fix it. This is a compile-time error: Java found the problem before running anything, which is one of Java's strengths.
Quick quiz
What is the entry point of a Java program?
Try it yourself
Install a JDK and create Main.java with the program that prints your name and your favourite food on two separate lines. Compile and run it. Then remove a semicolon on purpose and read the compiler's error message.
Summary
- Java source (
.java) is compiled to bytecode, which the JVM runs on any operating system. - All code lives in classes, and
mainis where a program starts. - Statements end with
;, and blocks use{ }. Java is case sensitive. javac Main.javathenjava Mainbuilds and runs a program.java Main.javadoes both in one go.
Lesson complete
+20 XP
Nice work. Keep going while it is fresh.
Sources and further reading