Writing Your First Java Program – Structure, Syntax, and Basics


Table of Contents

  1. Java Program Structure Overview
  2. Creating a Simple Java Program
  3. Anatomy of a Java Program
  4. Understanding the main Method
  5. Packages and Class Names
  6. Java Comments (Single-line & Multi-line)
  7. Compiling and Running Java Code
  8. Common Compilation Errors and Fixes
  9. Summary and What’s Next

1. Java Program Structure Overview

Every Java program is made up of classes and methods, and follows a well-defined structure. Even a simple “Hello, World!” program gives a glimpse into how organized Java code must be.


2. Creating a Simple Java Program

Let’s start with a basic example. Save the following code in a file named HelloWorld.java.

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Now let’s understand what’s happening line by line.


3. Anatomy of a Java Program

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

public class HelloWorld

  • Defines a class named HelloWorld.
  • In Java, all code must reside inside a class.
  • The file name must match the public class name (HelloWorld.java).

public static void main(String[] args)

  • This is the entry point of any Java application.
  • public: Accessible from outside the class.
  • static: Can be run without creating an instance of the class.
  • void: Returns nothing.
  • String[] args: Accepts command-line arguments.

System.out.println("Hello, World!");

  • Prints the string to the console.
  • System.out is a built-in Java object that sends output to the standard output stream.

4. Understanding the main Method

The main() method is where execution starts. Without it, the JVM doesn’t know where to begin running your program.

Signature of the main method:

public static void main(String[] args)

If this signature is changed, the program won’t compile or run correctly.


5. Packages and Class Names

Java classes are typically organized into packages.

package com.example.app;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello from a package!");
}
}
  • Packages help organize large codebases.
  • They follow a directory structure.
  • A file with the above package would reside in com/example/app/HelloWorld.java.

6. Java Comments

Comments are ignored by the compiler but useful for documentation.

Single-line Comments

// This is a single-line comment

Multi-line Comments

/*
This is a multi-line comment
spanning multiple lines
*/

Documentation Comments (for Javadoc)

/**
* This is a documentation comment
* Used to describe classes and methods
*/

7. Compiling and Running Java Code

Step 1: Compile the code

javac HelloWorld.java

This generates a HelloWorld.class file (bytecode).

Step 2: Run the bytecode

java HelloWorld

Note: You don’t include the .class or .java extension when running.


8. Common Compilation Errors and Fixes

ErrorCauseFix
class HelloWorld is public, should be declared in a file named HelloWorld.javaMismatch between class name and file nameRename the file to match class
main method not foundWrong method signatureEnsure exact syntax: public static void main(String[] args)
cannot find symbolTypo in class/method/variable nameDouble-check spelling and declarations

9. Summary and What’s Next

In this module, you learned:

  • The structure and syntax of a basic Java program
  • How to write, compile, and run Java code
  • What each part of the program does
  • How comments and packages work