What Is It?
What Is a Java Program?
A Java program is a collection of instructions written in the Java programming language that the computer can execute. Every Java program consists of at least one class, and every standalone program must have a special method called main that serves as the entry point -- the place where execution begins.
Unlike scripting languages like Python where you can write a single line of code and run it, Java requires a specific structure. Your code must live inside a class, and the executable code must be inside methods. This structure may feel verbose at first, but it enforces organization and makes large programs manageable.
In this chapter, you will write your first Java program, understand every keyword in the public static void main(String[] args) signature, learn how to print output, add comments, follow naming conventions, and understand what happens when your code is compiled and run.
Why Does It Matter?
Why Is Understanding Program Structure Important?
Every Java program you ever write will follow the same basic structure. Understanding it thoroughly now prevents confusion later and builds a solid foundation for everything that follows.
1. The main Method Is Where Everything Starts
When you run a Java program, the JVM looks for the main method with a very specific signature. If this method is missing, has the wrong signature, or is misspelled, your program will not run. Understanding why each keyword (public, static, void) is required helps you understand Java's design philosophy.
2. Output Is How Programs Communicate
System.out.println is the most basic way for a program to display results. You will use it in every chapter, every exercise, and every debugging session. Understanding the difference between println, print, and printf gives you control over how your output looks.
3. Comments Make Code Maintainable
Professional code always includes comments. In placement coding rounds, well-commented solutions demonstrate clarity of thought. In team environments, comments help other developers (and your future self) understand your logic.
4. Naming Conventions Are Non-Negotiable
Java has strict naming conventions that the entire industry follows. Using camelCase for variables, PascalCase for classes, and UPPER_SNAKE_CASE for constants is not optional -- it is expected in interviews, code reviews, and professional codebases.
Detailed Explanation
Detailed Explanation
1. Anatomy of a Java Program
Let us dissect the simplest possible Java program line by line:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Line 1: public class HelloWorld {
public-- This is an access modifier that makes the class accessible from anywhere. Every Java source file can have at most one public class, and the file name must match this class name.class-- This keyword declares a class. In Java, all code must be inside a class. A class is a blueprint that groups related data and methods.HelloWorld-- This is the class name. It must match the file name exactly (HelloWorld.java). By convention, class names use PascalCase (each word capitalized).{-- The opening curly brace marks the beginning of the class body.
Line 2: public static void main(String[] args) {
This is the main method signature. Every keyword here is required and has a specific purpose:
public-- The method must be accessible by the JVM from outside the class. If you make itprivate, the JVM cannot call it.static-- The method belongs to the class itself, not to an instance (object) of the class. The JVM callsmainwithout creating an object, so it must be static.void-- The method does not return any value. Themainmethod performs actions but does not return a result to the JVM.main-- This is the method name. The JVM specifically looks for a method namedmain. You cannot change this name.String[] args-- This is a parameter: an array of Strings that receives command-line arguments. Even if you do not use command-line arguments, this parameter must be present. You can also write it asString args[]orString... args.
Line 3: System.out.println("Hello, World!");
System-- A built-in class in thejava.langpackage that provides access to system-level resources.out-- A static field of the System class, representing the standard output stream (typically the console/terminal).println-- A method that prints the given text followed by a newline character. "println" stands for "print line.""Hello, World!"-- A String literal enclosed in double quotes.;-- Every statement in Java must end with a semicolon. This is required, not optional.
Line 4 and 5: Closing Braces
The first } closes the main method. The second } closes the class. Every opening brace must have a matching closing brace.
2. Printing Output: println vs print vs printf
Java provides three primary methods for console output:
System.out.println()
Prints the argument followed by a newline character. The cursor moves to the next line after printing. This is the most commonly used output method.
System.out.print()
Prints the argument without a newline. The cursor stays on the same line. Useful when you want multiple outputs on the same line.
System.out.printf()
Prints formatted output using format specifiers, similar to C's printf. Common format specifiers:
| Specifier | Type | Example |
|---|---|---|
%s | String | printf("%s", name) |
%d | Integer | printf("%d", age) |
%f | Float/Double | printf("%.2f", gpa) |
%n | Newline (platform-independent) | printf("Hello%n") |
%b | Boolean | printf("%b", true) |
3. Comments in Java
Comments are text that the compiler ignores. They exist solely for humans reading the code.
Single-Line Comments
Start with //. Everything after // on that line is ignored by the compiler.
// This is a single-line comment
int age = 20; // This comment is after codeMulti-Line Comments
Start with /* and end with */. Can span multiple lines.
/* This is a multi-line comment.
It can span several lines.
Useful for longer explanations. */Javadoc Comments
Start with /** and end with */. Used to generate API documentation with the javadoc tool.
/**
* Calculates the area of a rectangle.
* @param length the length of the rectangle
* @param width the width of the rectangle
* @return the calculated area
*/4. Java Naming Conventions
Java has well-established naming conventions that every professional developer follows:
| Element | Convention | Example |
|---|---|---|
| Classes | PascalCase | StudentRecord, BankAccount |
| Methods | camelCase | calculateTotal(), getName() |
| Variables | camelCase | studentName, totalMarks |
| Constants | UPPER_SNAKE_CASE | MAX_SIZE, PI_VALUE |
| Packages | lowercase | com.example.app |
5. Escape Sequences
Escape sequences allow you to include special characters in strings that cannot be typed directly:
| Escape Sequence | Meaning | Example Output |
|---|---|---|
\n | Newline | Line break |
\t | Tab | Horizontal tab |
\\ | Backslash | \ |
\" | Double quote | " |
\' | Single quote | ' |
\r | Carriage return | Cursor to line start |
6. The Compilation Process
When you compile and run a Java program, the following steps happen:
- Source Code: You write
HelloWorld.javacontaining human-readable Java code - Compilation:
javac HelloWorld.javainvokes the Java compiler, which: parses the source code, checks for syntax and type errors, and generatesHelloWorld.classcontaining bytecode - Loading:
java HelloWorldstarts the JVM, which uses the Class Loader to loadHelloWorld.classinto memory - Verification: The bytecode verifier checks the code for safety and correctness
- Execution: The execution engine (interpreter + JIT compiler) runs the bytecode, starting from the
mainmethod
7. Command-Line Arguments
The String[] args parameter in the main method receives arguments passed from the command line. When you run java HelloWorld Aarav 20, the args array contains ["Aarav", "20"]. This allows your program to accept input without interactive prompts.
Code Examples
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}HelloWorld must match the file name HelloWorld.java. The main method is the entry point. System.out.println prints text followed by a newline. The semicolon terminates the statement. Compile with javac HelloWorld.java, run with java HelloWorld.public class PrintDemo {
public static void main(String[] args) {
// println - prints with newline
System.out.println("Line 1");
System.out.println("Line 2");
// print - no newline, stays on same line
System.out.print("Hello ");
System.out.print("World");
System.out.println(); // just a newline
// printf - formatted output
String name = "Aarav";
int age = 20;
double gpa = 8.75;
System.out.printf("Name: %s, Age: %d, GPA: %.2f%n", name, age, gpa);
// printf with width formatting
System.out.printf("%-10s %5d %8.2f%n", "Priya", 21, 9.10);
System.out.printf("%-10s %5d %8.2f%n", "Rohan", 19, 7.85);
}
}println adds a newline after each call, so "Line 1" and "Line 2" appear on separate lines. print does not add a newline, so "Hello " and "World" appear on the same line. printf uses format specifiers: %s for strings, %d for integers, %.2f for doubles with 2 decimal places, %n for a platform-independent newline. The %-10s left-aligns in a 10-character field; %5d right-aligns in a 5-character field./**
* StudentGreeter - Demonstrates all three comment types in Java.
* This Javadoc comment can be used to generate documentation.
* @author Priya
* @version 1.0
*/
public class StudentGreeter {
// This is a single-line comment
// The main method is the program's entry point
public static void main(String[] args) {
/* Multi-line comment:
We declare a variable and use it
to create a personalized greeting. */
String studentName = "Vikram";
int semester = 4;
// Print the greeting
System.out.println("Welcome, " + studentName + "!");
System.out.println("Semester: " + semester);
// TODO: Add more student details later
}
}/** */) at the top describe the class and can include tags like @author and @version. Single-line comments (//) explain individual lines or sections. Multi-line comments (/* */) span several lines for longer explanations. The TODO comment is a common convention for marking code that needs future work -- most IDEs highlight these.public class Concatenation {
public static void main(String[] args) {
String firstName = "Ananya";
String lastName = "Sharma";
int age = 19;
double percentage = 87.5;
// String concatenation with +
System.out.println("Name: " + firstName + " " + lastName);
System.out.println("Age: " + age);
System.out.println("Score: " + percentage + "%");
// Tricky: numbers and strings
System.out.println(10 + 20 + " students"); // 30 students (math first)
System.out.println("Roll: " + 10 + 20); // Roll: 1020 (concatenation)
System.out.println("Sum: " + (10 + 20)); // Sum: 30 (parentheses force math)
}
}+ operator concatenates strings. When you write "Age: " + age, Java automatically converts age (int) to a String and concatenates. The tricky part: 10 + 20 + " students" evaluates left to right -- first 10 + 20 = 30 (integer addition), then 30 + " students" = "30 students" (concatenation). But "Roll: " + 10 + 20 becomes "Roll: 10" + 20 = "Roll: 1020" because the first + triggers string concatenation, and it continues. Use parentheses to force arithmetic: "Sum: " + (10 + 20) gives "Sum: 30".public class EscapeDemo {
public static void main(String[] args) {
// Newline
System.out.println("Line 1\nLine 2\nLine 3");
// Tab (useful for alignment)
System.out.println("Name\tAge\tCity");
System.out.println("Aarav\t20\tDelhi");
System.out.println("Neha\t19\tMumbai");
// Quotes inside strings
System.out.println("He said, \"Java is great!\"");
// Backslash
System.out.println("File path: C:\\Users\\Aarav\\code");
// Single quote (not strictly necessary in strings, but valid)
System.out.println("It\'s a sunny day");
}
}\n inserts a newline within a single string, splitting it across lines. \t inserts a tab character, useful for creating aligned columns. \" allows you to include double quotes inside a string (without it, the compiler would think the string ends). \\ produces a single backslash (needed because backslash is the escape character itself). These escape sequences work in both println and printf.public class CommandArgs {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("First argument: " + args[0]);
}
if (args.length > 1) {
System.out.println("Second argument: " + args[1]);
}
// Print all arguments
System.out.println("\nAll arguments:");
for (int i = 0; i < args.length; i++) {
System.out.println(" args[" + i + "] = " + args[i]);
}
}
}
// Run: java CommandArgs Aarav 20 Delhi
// Output: First argument: Aarav, Second: 20, etc.String[] args parameter captures command-line arguments. If you run java CommandArgs Aarav 20 Delhi, then args[0] is "Aarav", args[1] is "20" (as a String, not an int), and args[2] is "Delhi". args.length gives the count. Note that all arguments are Strings; if you need a number, you must parse it (e.g., Integer.parseInt(args[1])).// File: GreetingApp.java
// Only ONE class can be public, and it must match the file name
public class GreetingApp {
public static void main(String[] args) {
String message = MessageHelper.getGreeting("Rohan");
System.out.println(message);
System.out.println("Program executed successfully.");
}
}
// This class is NOT public (default access)
// It can be in the same file as GreetingApp
class MessageHelper {
static String getGreeting(String name) {
return "Hello, " + name + "! Welcome to Java programming.";
}
}public, and the file name must match that public class. The MessageHelper class is package-private (no access modifier), so it can exist in the same file. When compiled, this produces two .class files: GreetingApp.class and MessageHelper.class. In practice, each class usually gets its own file for better organization.public class NamingConventions {
// Constant: UPPER_SNAKE_CASE
static final double PI_VALUE = 3.14159;
static final int MAX_STUDENTS = 60;
public static void main(String[] args) {
// Variable: camelCase
String studentName = "Neha";
int rollNumber = 42;
double semesterGpa = 8.9;
boolean isPlaced = true;
// Method call: camelCase
displayStudentInfo(studentName, rollNumber, semesterGpa, isPlaced);
}
// Method: camelCase
static void displayStudentInfo(String name, int roll, double gpa, boolean placed) {
System.out.println("=== Student Info ===");
System.out.println("Name: " + name);
System.out.println("Roll: " + roll);
System.out.println("GPA: " + gpa);
System.out.println("Placed: " + placed);
System.out.println("Max Students: " + MAX_STUDENTS);
}
}
// Class name: PascalCase (NamingConventions)
// Package names: all lowercase (com.example.app)NamingConventions uses PascalCase. Variables like studentName and methods like displayStudentInfo use camelCase. Constants like PI_VALUE and MAX_STUDENTS use UPPER_SNAKE_CASE with the static final keywords. Following these conventions is critical for professional Java development and coding interviews.Common Mistakes
Missing Semicolon
public class Test {
public static void main(String[] args) {
System.out.println("Hello")
}
}public class Test {
public static void main(String[] args) {
System.out.println("Hello");
}
};). This is different from Python which uses newlines to end statements. The compiler's error message points to the exact location where the semicolon is missing. This is the single most common syntax error for beginners.Wrong main Method Signature
public class Test {
public void main(String[] args) { // Missing 'static'
System.out.println("Hello");
}
}public class Test {
public static void main(String[] args) {
System.out.println("Hello");
}
}public static void main(String[] args) exactly. If you omit static, the JVM cannot call the method without creating an object. If you change void to another return type, or change the parameter type, the JVM will not recognize it as the entry point. The error message is clear, but this mistake is common.Using println with Lowercase 'l'
public class Test {
public static void main(String[] args) {
System.out.printLn("Hello"); // Wrong: capital L
}
}public class Test {
public static void main(String[] args) {
System.out.println("Hello"); // Correct: lowercase l, lowercase n
}
}println (all lowercase), not printLn, Println, or PrintLn. Similarly, System must have a capital S, and out must be lowercase. The "cannot find symbol" error means Java does not recognize the name you typed.String Concatenation Surprise with Numbers
public class Test {
public static void main(String[] args) {
System.out.println("Score: " + 10 + 20);
// Expected: Score: 30
// Actual: Score: 1020
}
}public class Test {
public static void main(String[] args) {
System.out.println("Score: " + (10 + 20)); // Score: 30
}
}+ from left to right. "Score: " + 10 produces the string "Score: 10" (string concatenation). Then "Score: 10" + 20 produces "Score: 1020" (still concatenation). To force arithmetic first, use parentheses: (10 + 20) evaluates to 30 before being concatenated.Forgetting Curly Braces
public class Test {
public static void main(String[] args)
System.out.println("Hello");
}public class Test {
public static void main(String[] args) {
System.out.println("Hello");
}
}{ }. Without the opening brace after the method signature, the compiler gets confused and gives a misleading error. Always ensure every opening brace { has a matching closing brace }. IDEs auto-insert matching braces, but it is important to understand the requirement.Using Single Quotes for Strings
public class Test {
public static void main(String[] args) {
System.out.println('Hello World'); // Single quotes!
}
}public class Test {
public static void main(String[] args) {
System.out.println("Hello World"); // Double quotes for strings
System.out.println('H'); // Single quotes for single characters only
}
}"") denote Strings, and single quotes ('') denote a single character (char). 'Hello World' is invalid because single quotes can only contain one character. This is different from Python where single and double quotes are interchangeable for strings.Summary
- Every Java program must have at least one class. All executable code must be inside methods within a class.
- The main method signature is exactly: public static void main(String[] args). Each keyword is required: public (JVM access), static (no object needed), void (no return value), String[] args (command-line arguments).
- System.out.println() prints text followed by a newline. System.out.print() prints without a newline. System.out.printf() prints formatted output using specifiers like %s, %d, %.2f.
- Java has three comment types: single-line (//), multi-line (/* */), and Javadoc (/** */). Use comments to explain why, not what.
- The file name must exactly match the public class name (case-sensitive). HelloWorld class must be in HelloWorld.java.
- Java naming conventions: PascalCase for classes (StudentRecord), camelCase for variables and methods (studentName, getAge), UPPER_SNAKE_CASE for constants (MAX_SIZE).
- Escape sequences: \n (newline), \t (tab), \" (double quote), \\ (backslash). These work inside string literals.
- String concatenation with + converts non-String values to Strings automatically. Use parentheses around arithmetic to prevent unexpected concatenation: "Sum: " + (10 + 20) gives "Sum: 30".
- Every statement ends with a semicolon (;). Missing semicolons are the most common syntax error for beginners.
- Java is case-sensitive: System (capital S), println (lowercase), main (lowercase). Typos in these names cause 'cannot find symbol' errors.