First Java Program

Introduction

The fastest way to become comfortable with Java is to write a small program, compile it, and run it from beginning to end. In this article, you will create a simple Java program, understand its basic structure, learn what the main method does, compile the source code into bytecode, and run it using the Java launcher. The example uses a small e-commerce application called HelloShop, which can later be extended with products, prices, carts, and orders.

Writing the Source Code

Create a file named HelloShop.java using a text editor or your preferred IDE and add the following program:

This program defines a class named HelloShop, contains the main method that acts as the entry point of the application, and prints two messages to the terminal. Java is case-sensitive, so capitalization matters. System and system are different names, statements normally end with semicolons, and opening and closing braces must be properly matched.

Understanding the Program Structure

The first line declares a public class named HelloShop. In Java, code is organized inside classes, and when a class is declared public, the source file must have the same name as the class. The outer braces define the body of the class, while the inner braces define the body of the main method. The main method is the entry point used when starting a standalone Java application.

The main method contains five important parts: public makes the method accessible from outside the class, static allows the JVM to call it without creating an object first, void indicates that the method does not return a value, main is the method name expected by the JVM, and String[] args represents the command-line arguments passed to the program. Together, these parts form the standard entry point for a Java application.

Understanding the Output Statement

The println statement is responsible for displaying text in the terminal. System refers to a standard Java class, out represents the standard output stream, and println prints the supplied text followed by a new line. The text inside double quotation marks is a String literal.

Both println statements work in the same way, which is why the program produces two lines of output. As you continue learning Java, these basic statements will be replaced and combined with variables, objects, conditions, loops, collections, and other programming concepts.

Saving the Java File

The file must be named HelloShop.java because the program contains a public class named HelloShop. The spelling and capitalization must match exactly.

Public ClassRequired File Name
HelloShopHelloShop.java
CartTotalCartTotal.java
OrderListOrderList.java

For example, saving the program as helloshop.java or Helloshop.java can cause a compilation error because the file name does not match the public class name.

Compiling the Program

Java source code is not executed directly by the JVM in the traditional compilation workflow. First, the Java compiler converts the source code into bytecode. The compiler is provided by the JDK and is commonly invoked using the javac command.

Open a terminal, move to the directory containing HelloShop.java, and run:

javac HelloShop.java

If the source code is correct, the compiler normally produces no output and creates a new file named HelloShop.class. This class file contains the bytecode that the JVM can execute.

You can check whether the class file was created by listing the contents of the directory:

ls

On Windows, you can use:

dir

Running the Program

After compilation, use the Java launcher to execute the compiled class.

java HelloShop

The output will be:

Welcome to MyShopHappy Shopping!

When using the Java launcher in this traditional workflow, provide the class name rather than the class file name. Therefore, use java HelloShop rather than java HelloShop.class. The terminal also needs to be in the directory containing the compiled class file, unless the classpath has been configured to locate it elsewhere.

Command-Line Arguments

The args parameter in the main method allows a program to receive values from the command line. For example, the program can be started with additional arguments:

java HelloShop coupon10 freeship

The first argument becomes args[0], while the second becomes args[1]. The current HelloShop program does not use these values, so its output remains unchanged, but command-line arguments become useful when building programs that accept configuration or user-provided values.

Java Source Code to Program Execution

The complete process is straightforward: you write Java source code, compile it using the Java compiler, obtain bytecode, and then use the Java launcher to start the JVM and execute that bytecode. The compiler performs tasks such as parsing the source code, checking types, and generating bytecode, while the JVM loads and executes the resulting bytecode. For now, you can think of the compiler as the tool that transforms Java source code into bytecode and the JVM as the runtime that executes it.

Running the Program in an IDE

Although the terminal shows exactly how the Java compilation and execution process works, most developers use an Integrated Development Environment for everyday development. IntelliJ IDEA, Eclipse, and Visual Studio Code provide tools for writing, compiling, running, debugging, and managing Java projects.

In IntelliJ IDEA, create a Java project and select the JDK you installed earlier. Create a Java class named HelloShop inside the source directory and add the program. IntelliJ will provide a run option next to the main method. Selecting it compiles and runs the application and displays the output in the IDE's Run window. Eclipse and Visual Studio Code provide similar functionality, although the interface and controls are different.

Common Errors and Fixes

Most beginner errors are caused by spelling mistakes, incorrect capitalization, missing semicolons, mismatched braces, or incorrect file names. Understanding the compiler message is an important part of learning Java because it tells you where the compiler found a problem and often gives you a useful description of what went wrong.

Mismatched File Name

If the source contains a public class named HelloShop but the file is saved with a different name, compilation will fail. Rename the file so that it exactly matches the public class name.

The correct file name is HelloShop.java.

Misspelled System

Java is case-sensitive, and names must be spelled correctly. For example, the following code contains a spelling mistake:

The compiler does not recognize Sytem because no such class is available. Correcting the spelling fixes the problem:

System.out.println("Welcome to MyShop");

A cannot find symbol error is one of the most common errors beginners encounter. It often points to a misspelled class, method, or variable name, so carefully check the spelling and capitalization of the name mentioned by the compiler.

Missing Semicolon

Java statements generally end with a semicolon. Forgetting one can produce a compilation error.

System.out.println("Welcome to MyShop") 

The corrected statement is:

System.out.println("Welcome to MyShop");

Incorrect Main Method

The JVM looks for the standard main method when starting a standalone Java application. If the method is incorrectly written, the program may compile but fail when you try to run it.

For example, changing main to Main creates a different method:

The correct entry point is:


The name, access modifier, static modifier, return type, and parameter list are important because the JVM expects the standard entry-point signature.

Incorrect Java Command

Another common mistake is including the class file extension when running the program.

Incorrect:

java HelloShop.class

Correct:

java HelloShop

The Java launcher expects the class name in this form. You may also see an error if you run the command from a directory where the compiled class cannot be found. In that situation, move to the directory containing HelloShop.class or configure the appropriate classpath.

Single-File Java Programs

Modern Java also supports running a single source file directly. For a small program, you can use:

java HelloShop.java

The JDK handles the compilation and execution as part of the command. This is convenient for small programs and quick experiments, while the traditional compile-then-run process remains important for understanding how Java applications are developed and executed.

First Program Best Practices

Keep your first programs small and focus on understanding how source code, compilation, and execution work together. Pay attention to capitalization, class names, file names, braces, and semicolons because these details form the basic syntax of Java. When the compiler reports an error, read the message carefully instead of immediately changing multiple lines. Fix one problem at a time and compile again.

It is also useful to practice both terminal-based execution and IDE-based execution. The terminal helps you understand what the Java compiler and runtime are actually doing, while an IDE provides productivity features that become increasingly useful as your programs and projects grow.

Interview Tip

A common interview question is: What happens when you run a Java program?

A strong answer is: Java source code is first compiled by the Java compiler into bytecode. The Java launcher starts the JVM, which loads and verifies the bytecode and executes it. The JVM can interpret the bytecode and use Just-In-Time compilation to optimize frequently executed code during runtime.

Another common question is: Why does the Java file name have to match the class name?

When a class is declared public, Java requires the source file name to match the public class name exactly. For example, a public class named HelloShop must be stored in a file named HelloShop.java.

Key Takeaways

  • A Java program is written as source code and stored in a Java source file.
  • A public class must have the same name as its source file.
  • The main method is the standard entry point of a standalone Java application.
  • Java is case-sensitive.
  • The Java compiler converts source code into bytecode.
  • The JVM executes the generated bytecode.
  • The Java launcher starts a Java application.
  • Command-line arguments are available through the args parameter.
  • Common beginner errors include incorrect file names, spelling mistakes, missing semicolons, and incorrect main methods.
  • Modern Java can also run a single source file directly.

Conclusion

Your first Java program introduces the complete development cycle you will use throughout your Java journey: write source code, compile it into bytecode, and run it using the JVM. Although the HelloShop program is small, it introduces several fundamental Java concepts, including classes, the main method, statements, standard output, compilation, bytecode, and program execution.

As you continue learning, the same structure will become the foundation for larger applications. HelloShop can eventually evolve from a simple program that prints messages into an application containing variables, objects, methods, collections, conditions, loops, products, shopping carts, and orders. Understanding this first program clearly will make those later concepts much easier to follow.