Your First Java Program: Source, Bytecode and the JVM
Write, compile and run a first Java program, see what the compiler produces and why Java code runs on a virtual machine, with a real class file inspected byte by byte.
In this lesson you will
- Write and run a Java program
- Explain source code, bytecode and the JVM
- Read the main method
- Inspect a real compiled class file
About 28 minutes
Hana wants a program that counts her bikes
Hana runs a small bike rental shop and keeps her bike list on paper. She wants a program that shows today’s numbers, and she has heard that Java is a good first language because it is used everywhere from banks to Android apps. Java is a typed, object-oriented language, which means that you tell the computer what kind of thing each value is, and you organise your programs into classes. This first lesson runs one tiny program and looks at what really happens between your text file and the screen.
Source, bytecode and the virtual machine
A Java program starts as source code, a text file that ends in .java. A compiler called javac translates it into bytecode, stored in a .class file. Bytecode is not instructions for a particular processor. It is instructions for the Java Virtual Machine, the JVM, a program that exists for Windows, macOS and Linux alike. The same class file runs on all of them, which is the idea behind the old slogan “write once, run anywhere”. Since Java 11 you can also run a single source file directly, and the launcher compiles it in memory first.
| Step | Tool | Result |
|---|---|---|
| Write | Any text editor | Main.java |
| Compile | javac | Main.class (bytecode) |
| Run | java | Output on the screen, produced by the JVM |
Every Java program begins at a method called main. Its header looks strange at first: public static void main(String[] args). The word public means that anyone may call it, static means that it belongs to the class and not to an object, void means that it returns nothing, and String[] args is a list of text values that you can pass on the command line. You will use each part properly in later lessons, and for now you can treat the header as the doorway that the JVM looks for.
Run it
The program below prints three lines. The last one asks the JVM which feature version of Java is running, so the output shows the real version of this course environment.
public class Main {
public static void main(String[] args) {
String shop = "Hana's Bikes";
int bikes = 12;
System.out.println("Welcome to " + shop);
System.out.println("Bikes available: " + bikes);
System.out.println("Running on Java " + Runtime.version().feature());
}
}
Welcome to Hana's Bikes
Bikes available: 12
Running on Java 21
The program printed the shop name, the 12 bikes and the line “Running on Java 21”, so the JVM that ran it is version 21. Notice that you did not tell it where to print: System.out is the standard output stream, and println writes a line to it and moves to the next line.
To see the bytecode side, the course compiled a similar file with javac and read the first bytes of the class file. They are CAFEBABE, a fixed number that every Java class file starts with, followed by the version. The class file reports major version 65, and the rule is that Java N has major version N + 44, so Java 21 should give 65, and it does. The whole file is only 253 bytes, because an empty main method needs very little. This is the proof that javac made a real, versioned file and not something hidden: the JVM checks those bytes before it runs anything, and an old JVM refuses a class file with a newer major version.
Things that trip beginners
- The file name must match the public class name, so Main.java contains class Main.
- Java is case sensitive: Main and main are different names.
- Every statement ends with a semicolon.
- Text goes in double quotes and single characters go in single quotes.
- Curly braces open and close blocks, and indentation is for people, not for Java.
If the file is called Main.java and the class inside is called Shop, the compiler refuses with a message about the public class name. Rename one of them so that they agree.
One missing semicolon can cause several messages. Fix the first one and compile again before you worry about the rest.
Every program in this lesson was compiled and run with OpenJDK 21 while the course was written. Java releases twice a year, so check the official Java documentation for the current version and for features added after Java 21.
Practice
Change the shop name and the number of bikes and run the program again. Then add a fourth line that prints your own message. Finally, remove a semicolon on purpose, compile, and read the error message carefully, since reading compiler messages is a skill that you will use every day.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does javac produce?
- A finished Windows program
- Bytecode in a .class file
- A web page
- A text summary
The JVM then runs the bytecode.
What is the JVM?
- A program that runs Java bytecode
- A text editor
- A compiler for C
- A database
It exists for many operating systems.
What does static mean in the main method header?
- It belongs to the class, not to an object
- It cannot be changed
- It is private
- It returns a number
The JVM calls main without creating an object first.
How did the course check that javac made a real class file?
- By guessing
- By its file name
- By its colour
- By reading the first bytes and the version
The magic number is CAFEBABE.
What must match the public class name?
- The output text
- The number of bikes
- The folder size
- The file name
Main.java holds class Main.
Summary
- Java source files are compiled to bytecode, and the JVM runs the bytecode.
- A program starts in the main method.
- A class file starts with the bytes CAFEBABE and a version equal to the Java version plus 44.
- The file name must match the public class name.
Questions
Do I need to install anything?
You need a Java Development Kit, JDK, which includes javac and java. The course uses a current long-term support release. Check the official Java site for downloads.
Is Java the same as JavaScript?
No. They are different languages that share part of a name.
Why does the output show a Java version?
Because the program asked the running JVM for it, which is a handy way to confirm which Java you are using.
