On this page (11 sections)
Java for Beginners · Module 1: Getting Started
🎯 Why Study a Tiny Program So Closely?
The first program looks small, but it contains the skeleton of every Java program you will ever write. Learning it line by line is like learning the parts of a bicycle before riding: once you know them, nothing surprises you later.
main with exactly the right signature.1️⃣ The Smallest Program
public class FirstProgram {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello, World!
public static void main(String[] args) {
System.out.println(“Hello, World!”);
}
}
| Part | Meaning |
| public class FirstProgram | Class declaration: every Java program lives in a class. The file must be FirstProgram.java. |
| public static void main(String[] args) | The main method: the JVM starts here. |
| System.out.println(“Hello, World!”); | A statement: prints text, then a new line. It ends with a semicolon. |
| { } | Braces mark where the class and the method begin and end. |
Figure 1: Anatomy of the smallest Java program.
2️⃣ The Class
classis the Java word for a blueprint. All Java code lives inside classes.publicmeans other code can see this class.- The class name is
FirstProgram. By convention, class names start with a capital letter and use CamelCase:StudentCard,BankAccount. - The file name must be
FirstProgram.java, the same as the public class.
3️⃣ The main Method
When you type java FirstProgram, the JVM searches the class for main and starts there. The signature must match exactly.
anyone can call it, so the JVM can
runs without creating an object first
returns nothing
the fixed name the JVM looks for
words typed after the class name
Figure 2: The five words of the main method signature.
- public: the JVM is outside your class, so it needs permission to call
main. - static: the JVM has not created any object yet, so
mainmust belong to the class itself. - void:
maindoes not give a value back. - String[] args: an array of words typed after the class name on the command line (you used it in Lesson 2).
4️⃣ Printing with System.out
a class from the standard library
the standard output (your screen)
method: print text, then newline
"Hello"what to print
Figure 3: System.out.println(“Hello”) taken apart.
System.out.println(x)printsxand then moves to a new line.System.out.print(x)printsxand stays on the same line.System.out.printf(format, values...)prints with a format, as in C. Use%dfor int,%ffor decimals,%sfor text and%nfor a new line.
Program 2: print, println, printf and escape characters
public class PrintStyles {
public static void main(String[] args) {
System.out.print("One ");
System.out.print("Two ");
System.out.println("Three");
System.out.println("Line A");
System.out.println("Name:\tAsha");
System.out.println("Quote: \"Java\" is fun");
System.out.println("Path: C:\\JavaPrograms");
System.out.printf("Pi is about %.3f%n", Math.PI);
System.out.println();
System.out.println("After a blank line");
}
}
One Two Three
Line A
Name: Asha
Quote: “Java” is fun
Path: C:\JavaPrograms
Pi is about 3.142
After a blank line
- The first three lines use
printtwice andprintlnonce, so “One Two Three” appears on one line. \tprints a tab, so there is a wide gap after “Name:”.- To print a double quote inside text, write
\". To print a backslash, write\\. %.3fprints three digits after the decimal point, so pi shows as 3.142.System.out.println()with nothing inside prints just an empty line.
| Escape | Meaning | Example | Prints |
| \n | new line | “A\nB” | A then B on the next line |
| \t | tab | “A\tB” | A, a wide space, B |
| \” | double quote | “Say \”Hi\”” | Say “Hi” |
| \\ | backslash | “C:\\Java” | C:\Java |
| %n | new line inside printf | printf(“A%n”) | A and a new line |
Figure 4: Common special characters inside Java text.
5️⃣ More Than One Class in a File
A file may contain several classes, but only one of them can be public, and the file name follows that class.
Program 3: A helper class (saved as Greeter.java)
class Helper {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
public class Greeter {
public static void main(String[] args) {
Helper.greet("Asha");
Helper.greet("Ravi");
}
}
Hello, Asha!
Hello, Ravi!
Helperis not public, so it can share the file withGreeter.Helper.greet("Asha")calls the static method through the class name.greettakes text and prints a greeting.- Run the program with
java Greeter, becauseGreeterhas themainmethod.
6️⃣ A Small Useful Program
public class StudentCard {
public static void main(String[] args) {
String name = "Asha Verma";
int roll = 21;
double cgpa = 8.75;
System.out.println("=== Student Card ===");
System.out.printf("Name : %s%n", name);
System.out.printf("Roll : %d%n", roll);
System.out.printf("CGPA : %.2f%n", cgpa);
System.out.println("====================");
}
}
=== Student Card ===
Name : Asha Verma
Roll : 21
CGPA : 8.75
====================
- The program stores three values in variables of types
String,intanddouble. You will study variables properly in Lesson 7. %s,%dand%.2fare placeholders thatprintffills with the variables, in order.
7️⃣ What the Compiler and JVM Say When Something Is Wrong
No static in main
public class NoStatic {
public void main(String[] args) {
System.out.println("Hello");
}
}
Error: Main method is not static in class NoStatic, please define the main method as:
public static void main(String[] args)
Wrong capital letter in main
public class WrongName {
public static void Main(String[] args) {
System.out.println("Hello");
}
}
Error: Main method not found in class WrongName, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
A missing closing brace
public class Unbalanced {
public static void main(String[] args) {
System.out.println("Hello");
}
Unbalanced.java:4: error: reached end of file while parsing
}
^
1 error
- Both
mainmistakes compile fine, butjavacannot start the program. These are runtime errors, not compile errors. - “Reached end of file while parsing” almost always means a
{was never closed. Count your braces.
⚠️ Common Mistakes
| Mistake | Problem | Fix |
|---|---|---|
public static void Main | Java looks for lowercase main | Write main |
Leaving out static | The JVM cannot call it | Use public static void main |
Missing ; | Statement is not finished | End every statement with ; |
System.out.println("Hi') | Mixed quote types | Text uses double quotes |
| Unbalanced braces | “reached end of file” | Match every { with a } |
| Class name and file name differ | Compile error | Make them identical |
❓ Frequently Asked Questions
Q1. Why is the program inside a class even for tiny programs?
Java is an object-oriented language, so all code must belong to a class.
Q2. Is String[] args required?
Yes in this form. You can rename args, but the type must stay String[].
Q3. What is the difference between println and printf?println prints a value and a new line. printf uses a format string, and you must add %n yourself for a new line.
Q4. Can I have two main methods?
One per class is normal. You choose which class to run with java ClassName.
✅ Key Takeaways
- A Java program is a class with a
public static void main(String[] args)method. - The file name must match the public class name.
printstays on the line,printlnends it, andprintfformats.- Use
\n,\t,\"and\\for special characters in text. - Statements end with
;and blocks use matching{ }. - Java is case-sensitive:
Mainis notmain, andsystemis notSystem.
🎓 Practice questions: (1) Explain each word inpublic static void main(String[] args). (2) Write a program that prints your name, branch and college on three lines. (3) Print the textHe said "Hi"using escape characters. (4) What is the difference between a compile error and the “Main method not found” error? (5) Useprintfto print the number 3.14159 with two decimal places.
All 30 lessons in this series (open course page)
Module 1 · Getting Started
- Lesson 1: What is Java? Features and Uses
- Lesson 2: Installing the JDK and Running Your First Program
- Lesson 3: JDK, JRE, JVM and WORA Explained
- Lesson 4: JVM Architecture and JIT Compilation
- Lesson 5: Hello World Program Explained Line by Line
Module 2 · Building Blocks
- Lesson 6: Comments, Identifiers and Keywords
- Lesson 7: Variables and Literals
- Lesson 8: Primitive Data Types in Java
- Lesson 9: Type Conversion and Casting
- Lesson 10: Taking Input with the Scanner Class
- Lesson 11: Escape Characters and Variable Scope in Java
Module 3 · Operators
- Lesson 12: Arithmetic Operators and Increment/Decrement in Java
- Lesson 13: Relational and Logical Operators in Java
- Lesson 14: Bitwise and Shift Operators in Java
- Lesson 15: Assignment, Ternary Operator and Operator Precedence in Java
Module 4 · Control Flow
- Lesson 16: if, if-else and else-if Ladder in Java
- Lesson 17: switch Statement in Java (Classic and Modern)
- Lesson 18: while and do-while Loops in Java
- Lesson 19: for Loop and Nested Loops (Patterns) in Java
- Lesson 20: break, continue and Labelled Loops in Java
Module 5 · Data and Methods
- Lesson 21: Arrays in Java (One-Dimensional)
- Lesson 22: Multidimensional Arrays in Java
- Lesson 23: Strings and the String Pool in Java
- Lesson 24: String Methods and StringBuilder in Java
- Lesson 25: Methods and Method Overloading in Java
- Lesson 26: Recursion Basics in Java
- Lesson 27: ArrayList and Wrapper Classes in Java
- Lesson 28: Exception Handling Basics in Java