Overview
This is the simplest Java program you can write. It prints a single line of text on the screen.
Every Java program you write later will follow this same basic structure: a class,
a main() method and statements inside it.
Save this file as MyFirstJavaProgram.java. In Java, the file name must exactly match the name of the public class, including capital letters.
Step-by-Step Explanation
1
Single-Line Comment
Notes for humans, ignored by the compiler.
// Print Your Name
Anything after // on the same line is a comment. It explains the code but isn't executed.
2
Class Declaration
Every Java program lives inside a class.
public class MyFirstJavaProgram {
...
}
public means the class can be accessed from anywhere. class is the keyword used to define a class,
and MyFirstJavaProgram is its name. The curly braces { } mark the start and end of the class.
3
Multi-Line Comment
Comments that span several lines.
/* This is my first Java program.
* It prints your name (or any text) as the output.
*/
Everything between /* and */ is a comment, however many lines it covers.
4
The main() Method
The entry point of every Java program.
public static void main(String[] args)
When you run the program, the JVM (Java Virtual Machine) looks for this exact method and starts executing from here.
5
Print Statement
Shows output on the screen.
System.out.println("rummanansari.com");
System.out.println() prints the text inside the double quotes and then moves the cursor to the next line.
Every statement in Java ends with a semicolon ;.
Parts of the main() Method
| Keyword |
Meaning |
public |
The JVM can call this method from outside the class. |
static |
The method can run without creating an object of the class. |
void |
The method doesn't return any value. |
main |
The name the JVM looks for to start the program. |
String[] args |
An array that holds command-line arguments, if any are passed. |
How to Compile and Run
javac MyFirstJavaProgram.java
java MyFirstJavaProgram
javac compiles the source code into bytecode and creates MyFirstJavaProgram.class.
java then runs that bytecode on the JVM. Don't add .class when running the program.
Output
rummanansari.com
Print Your Own Name
Replace the text inside the quotes with your name:
System.out.println("Rumman Ansari");
Common Mistakes
File name doesn't match the class name
Saving the file as myfirstjavaprogram.java or Program.java gives a compile error. It must be MyFirstJavaProgram.java.
Missing semicolon
Leaving out ; at the end of the print statement gives the error ';' expected.
Wrong capitalisation
Java is case-sensitive. system.out.println or Main won't work. Use System and main.
Good practice
Keep comments accurate. If you change what the program prints, update the comment too.
print() vs println()
| Method |
Behaviour |
System.out.print() |
Prints the text and keeps the cursor on the same line. |
System.out.println() |
Prints the text and moves the cursor to the next line. |
Key Takeaway
Every Java program needs a class and a main() method.
Use System.out.println() to print output, and always save the file with the same name as the public class.