Getting Started · Lesson 3 of 42
Your First Java Program
Every Java journey starts with "Hello, World!". It looks like a lot of words for one line of output, but each word has a job, and understanding them now will make the rest of the course much easier.
In this lesson
- The structure of a Java program
- What
public static void main(String[] args)means - Compiling and running from the terminal
- Printing, comments and reading compiler errors
Hello, World!
Create a file called Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello, World!
Compile and run it from the folder that contains the file:
javac Main.java # creates Main.class
java Main # runs it (no .class at the end)
Since Java 11 you can also run a single file directly with java Main.java – handy for small programs like the ones in this course.
Every word explained
The parts of the program
- public class Main { … }All code lives inside a class. A public class must be in a file with the same name: Main.java
- public static void main(String[] args)The entry point – the JVM starts running here
- System.out.println("…");A statement: print a line. Statements end with a semicolon
| Word | Meaning |
|---|---|
public | Visible from anywhere – the JVM must be able to call main. |
static | Belongs to the class itself, so the JVM can call it without creating an object. |
void | The method returns nothing. |
main | The special name the JVM looks for. |
String[] args | Command-line arguments, as an array of strings. |
System.out | The standard output stream (the console). |
Interview angle
"Why is main static?" Because no object of the class exists when the program starts; a static method can be called on the class directly. "Can we overload main?" Yes, but the JVM only calls the main(String[] args) version.
Printing: println, print and printf
public class Main {
public static void main(String[] args) {
System.out.println("Line one");
System.out.print("Same ");
System.out.print("line");
System.out.println(); // just a new line
System.out.println("Total: " + (40 + 2)); // + joins text and values
System.out.printf("%s scored %d marks (%.1f%%)%n", "Asha", 92, 92.0);
}
}
Line one Same line Total: 42 Asha scored 92 marks (92.0%)
printlnprints and moves to the next line;printstays on the same line.printfformats values:%stext,%dwhole number,%.1fdecimal with 1 place,%nnew line,%%a percent sign.
Common mistake: "Total: " + 40 + 2
Java evaluates + left to right. Once the left side is a string, the rest is joined as text: "Total: " + 40 + 2 prints Total: 402. Wrap the maths in brackets: "Total: " + (40 + 2).
Comments
public class Main {
/**
* Javadoc comment: documents classes and methods.
*/
public static void main(String[] args) {
// Single-line comment
/* Multi-line
comment */
System.out.println("Comments are ignored by the compiler");
}
}
Comments are ignored by the compiler
Rules you can't break
- Java is case-sensitive:
System≠system,Main≠main. - Every statement ends with
;. Blocks use{ }. - The public class name must match the file name exactly.
- Indentation is for humans; Java ignores it – but always indent consistently.
Reading compiler errors
The compiler tells you the file, the line and what it expected. Read the first error first – later errors are often caused by it.
public class Main {
public static void main(String[] args) {
System.out.println("Missing semicolon")
}
}
Main.java:3: error: ';' expected
System.out.println("Missing semicolon")
^
1 error| Error | Typical cause |
|---|---|
';' expected | Missing semicolon at the end of a statement |
cannot find symbol | Misspelt name, wrong capitalisation or missing import |
class X is public, should be declared in a file named X.java | File name and class name differ |
reached end of file while parsing | A missing closing brace } |
Practice
Try it yourself
Print your name, city and course on three lines.
Show solution
public class Main { public static void main(String[] args) { System.out.println("Rohan"); System.out.println("Pune"); System.out.println("B.Tech CSE"); } }Rohan Pune B.Tech CSE
Use
printfto print "Price: ₹499.00" from the number 499.Show solution
public class Main { public static void main(String[] args) { System.out.printf("Price: ₹%.2f%n", 499.0); } }Price: ₹499.00
Quick check
What is printed by System.out.println("A" + 1 + 2);?
A12 – after the string, + joins as text.
What must the file be called if it contains public class Invoice?
Invoice.java
Why is main static?
So the JVM can call it on the class without creating an object first.
Summary
- Code lives in classes; the JVM starts at
public static void main(String[] args). - Compile with
javac Main.java, run withjava Main(orjava Main.java). - Use
println,printandprintf; watch out for string + number order.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.