Your First Java Program
Every Java program you’ll ever write follows the same basic structure. Once you understand it, you’ll be able to create any program from scratch, and more importantly, you’ll be able to read and make sense of Java code you encounter in the wild. Right now that structure probably looks like a wall of confusing syntax, after this lesson, it’ll feel like a familiar template.
The simplest Java program
Here’s the smallest valid Java program that does something you can actually see:
If you run this, you’ll see:
Hello, world!That’s it. But let’s break down what each piece is doing, because almost all of it will appear in every program you write.
The class declaration
public class HelloWorld { ...}In Java, all code lives inside a class. Think of a class as a container. The name after class (here, HelloWorld) is the name you’re giving that container.
Two rules you can’t break:
- The class name must match the filename exactly. This class is named
HelloWorld, so the file must be namedHelloWorld.java. Java is strict about this. - Class names start with a capital letter by convention.
HelloWorld, nothelloworldorhello_world.
The curly braces { and } mark where the class starts and ends. Everything inside those braces belongs to the class.
You’ll hear more about what classes mean in later chapters. For now, treat it as required boilerplate.
Where your program begins
public static void main(String[] args) { ...}This block is special. When you run a Java program, Java looks for something that looks exactly like this and starts executing from here. Think of it as the front door of your program, execution always enters through it.
Every word in that line has a purpose, but most involve concepts you haven’t seen yet. For now, just recognize the shape and know that your program’s starting instructions go inside those curly braces.
The instruction
System.out.println("Hello, world!");This is a statement, a single instruction that tells Java to do something. This particular one prints a line of text to the terminal.
System.out.println is built into Java. You give it some text between the parentheses, and it prints that text followed by a newline. The text itself, "Hello, world!", is a string literal: a piece of text wrapped in double quotes.
Notice the semicolon
;at the end. In Java, every statement ends with a semicolon. Forgetting one is one of the most common beginner mistakes, and the compiler will tell you about it.
Compiling and running
Java is a compiled language. Before your program can run, it has to be translated from the code you write into a form Java can execute. This translation step is called compilation.
When you click “Run”, two things happen behind the scenes:
- The compiler reads your code and checks it for mistakes. If it finds any, it stops and shows you an error.
- If everything looks okay, it runs the program and shows you the output.
This is why Java can catch certain mistakes before your program even starts executing.
Try removing the semicolon at the end of the
printlnline and hit “Run”. Read the error you get; that’s a compile error, and you’ll see plenty of them.
A slightly more interesting version
Let’s make the program do a little more:
Output:
Loading... Done.Welcome to the grade calculator.You’ll notice a new instruction here: System.out.print. It’s similar to System.out.println, but with one difference: it doesn’t move to a new line after printing. The next output continues right where it left off on the same line. System.out.println adds a newline at the end; System.out.print doesn’t.
Each statement still executes top to bottom. Java doesn’t skip around, it reads your instructions exactly as you wrote them, one after another.
Also notice: the file for this program would be Greeter.java, not HelloWorld.java. The filename has to match the class name.
What to pay attention to
A few things new Java programmers consistently trip over:
Capitalization matters. System is not the same as system. Java is case-sensitive: every word, every letter, everywhere.
The file name must match the class name. If your class is Greeter, the file must be Greeter.java. A mismatch gives you a compile error immediately.
Every statement ends with a semicolon. It’s easy to forget when you’re typing fast. The compiler will tell you when you do, but it’s good to build the habit now.
Curly braces must balance. Every { needs a matching }. IDEs can show you mismatches, but reading your own code carefully is worth doing.
What you’ve built
At this point you have a working Java program. It’s not doing anything impressive yet, but the structure you just learned (class, main block, instructions) is the skeleton every Java program is built on. When you write something far more complex six chapters from now, it’ll still look like this at its core.
Checkpoint
Which of these is required for a Java program to run?
A student writes this program in a file named Greeter.java:
public class Welcome {
public static void main(String[] args) {
System.out.println("Hi!")
}
}
The program fails to compile. What are the likely reasons?