Writing Your First Java Program

Introduction

Now that you have set up your Java development environment, it’s time to write and run your first Java program. In this chapter, we will walk you through creating a simple Java program, compiling it, and running it on your computer.

Step 1: Create a New Java File

Choosing a Text Editor or IDE

Before you write your first Java program, you need a text editor or an Integrated Development Environment (IDE) to write your code. Some popular options include:

  • Text Editors: Notepad, Sublime Text, Visual Studio Code
  • IDEs: IntelliJ IDEA, Eclipse, NetBeans

For this chapter, we will use a simple text editor, but the steps are similar if you choose to use an IDE.

Creating the File

  1. Open Your Text Editor:
    • Open your preferred text editor or IDE.
  2. Create a New File:
    • Create a new file and name it HelloWorld.java. The file name must match the class name and should have a .java extension.

Step 2: Write the Java Code

In your HelloWorld.java file, type the following code:

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

Explanation of the Code

  • public class HelloWorld:
    • This line declares a public class named HelloWorld. In Java, every application must have at least one class definition.
  • public static void main(String[] args):
    • This line declares the main method, which is the entry point of any Java program. The main method is where the program starts execution.
  • System.out.println(“Hello, World!”);
    • This line prints the text “Hello, World!” to the console. System.out is an output stream used to print text, and println is a method that prints the specified string followed by a new line.

Step 3: Compile the Java Program

Before running your Java program, you need to compile it into bytecode that the Java Virtual Machine (JVM) can understand.

  1. Open Command Prompt or Terminal:
    • Open your command prompt (Windows) or terminal (macOS/Linux).
  2. Navigate to the Directory:
    • Use the cd command to navigate to the directory where you saved HelloWorld.java. For example:
      cd path/to/your/java/file
      
  3. Compile the Program:
    • Type the following command to compile your Java program:
      javac HelloWorld.java
      
    • If there are no errors in your code, this command will create a file named HelloWorld.class in the same directory.

Step 4: Run the Java Program

Now that your program is compiled, you can run it using the JVM.

  1. Run the Program:
    • In the command prompt or terminal, type the following command:
      java HelloWorld
      
    • You should see the output:
      Hello, World!
      

Troubleshooting

  • If you see an error saying javac or java is not recognized:
    • Make sure you have installed the JDK and set up your environment variables correctly.
  • If there are compilation errors:
    • Double-check your code for typos and ensure that the file name matches the class name.

Conclusion

In this chapter, you learned how to write, compile, and run a simple Java program. This foundational knowledge is crucial as you begin your journey into Java programming. With your development environment set up and your first program successfully executed, you are ready to explore more complex Java concepts and start building your own applications. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top