Your First Java Program
There is no better way to learn a language than to run code. In this guide you’ll write the classic “Hello, World!” program, compile it with javac, run it on the JVM, and understand exactly what every keyword in public static void main(String[] args) means.
The Program
Create a file named HelloWorld.java. The filename must match the public class name exactly — including capitalization.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Warning: Java is case-sensitive and the file name must match the public class.
helloworld.javaorHelloworld.javawill not compile against a class namedHelloWorld.
Compile and Run
Java is a compiled language: javac turns your source into bytecode, then java executes it on the JVM.
# 1. Compile HelloWorld.java -> HelloWorld.class (bytecode)
javac HelloWorld.java
# 2. Run the class (note: no .class extension)
java HelloWorld
Output:
Hello, World!
Tip: Since Java 11 you can run a single-file program without compiling first:
java HelloWorld.java. It’s perfect for quick experiments, though larger projects still compile explicitly.
Anatomy of the main Method
The line public static void main(String[] args) is the entry point the JVM looks for. Every word matters:
| Token | Meaning |
|---|---|
public | Access modifier — the JVM must be able to call it from outside the class. |
static | Belongs to the class, not an instance — the JVM calls it without creating an object. |
void | Returns nothing to the JVM. |
main | The exact method name the JVM searches for to start execution. |
String[] args | An array of command-line arguments passed to the program. |
If any of these are missing or misspelled, the program compiles but the JVM cannot start it.
Why static?
When the JVM launches your program, no objects exist yet. A static method can be invoked directly on the class (HelloWorld.main(...)) without first constructing a HelloWorld instance — solving the chicken-and-egg startup problem.
Using command-line arguments
The args array holds anything you type after the class name:
public class Greet {
public static void main(String[] args) {
String name = args.length > 0 ? args[0] : "World";
System.out.println("Hello, " + name + "!");
}
}
javac Greet.java
java Greet Ada
Output:
Hello, Ada!
Common Beginner Errors
1. Filename doesn’t match the public class
error: class HelloWorld is public, should be declared in a file named HelloWorld.java
Rename the file so it matches the public class exactly.
2. Running with the .class extension
java HelloWorld.class # WRONG
Error: Could not find or load main class HelloWorld.class
Run with just the class name: java HelloWorld.
3. Wrong main signature
If you write public void main(...) (missing static) or misspell it as Main, you’ll see:
Error: Main method not found in class HelloWorld
The signature must be exactly public static void main(String[] args).
4. Forgetting to compile after editing
The java command runs the last-compiled .class file. If your changes don’t appear, you forgot to re-run javac.
Note:
System.out.printlnprints a line and adds a newline;System.out.printomits the trailing newline.outis aPrintStreamobject representing standard output.
Best Practices
- Name the file identically to the public class it contains.
- Always recompile after editing source files.
- Keep one public top-level class per file.
- Reach for
jshellto experiment with snippets without the compile-run cycle.
Interview Questions
Why must the main method be static?
The JVM invokes main before any objects exist. A static method can be called directly on the class without instantiating it, making it a valid entry point.
What does String[] args represent?
It is an array of command-line arguments passed to the program when it is launched. args[0] is the first argument.
What is the difference between javac and java?
javac is the compiler that converts .java source into .class bytecode. java is the launcher that starts the JVM to execute that bytecode.