2.4 Lab - Hello World in Java

Introduction

In this lab, you will write, compile, and run your first Java program: "Hello, World!" The "Hello, World!" program is a simple program that displays the text "Hello, World!" on the screen. This is a common first step in learning any programming language. You will use the Nano editor in a Linux environment to write the Java code, compile it using the javac compiler, and then execute the program using the java command.

Objectives

By the end of this lab, you will be able to:

  1. Write a basic Java program using Nano.
  2. Compile the Java program in a Linux environment.
  3. Run the compiled Java program.
  4. Understand basic Java syntax, including the class definition and main method.
  5. Modify a Java program and recompile it.
  6. Work with comments and basic code structure.

Lab Steps

Step 1: Access the Linux Terminal
  1. Open the terminal on your Linux system.
  2. Ensure you are in your home directory by running the command:
    cd ~
    
Step 2: Create and Edit a Java File
  1. In the terminal, create a new Java file using the Nano editor:
    nano HelloWorld.java
    
  2. Type the following Java code into the file:
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
    
  3. Save the file by pressing CTRL + O, then hit Enter to confirm the filename.
  4. Exit Nano by pressing CTRL + X.
Step 3: Compile the Java Program
  1. In the terminal, compile the Java program using the javac command:
    javac HelloWorld.java
    
  2. If there are no errors, this command will create a file named HelloWorld.class. You can check that the file exists by running:
    ls
    
  3. If there are errors, return to Nano to edit the HelloWorld.java file and fix any mistakes.
Step 4: Run the Java Program
  1. Run the compiled program with the java command:
    java HelloWorld
    
  2. You should see the output:
    Hello, World!
    
Step 5: Modify the Program
  1. Open the HelloWorld.java file again using Nano:
    nano HelloWorld.java
    
  2. Modify the message inside the System.out.println() statement, for example:
    System.out.println("Hello, Java Programming!");
    
  3. Save and exit the file, recompile using javac, and run the program again to see the new output:
    javac HelloWorld.java
    java HelloWorld
    
Step 6: Add Comments to Your Code
  1. Reopen the HelloWorld.java file:
    nano HelloWorld.java
    
  2. Add a comment at the top of the program to describe what it does:
    // This program prints "Hello, World!" to the console.
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
    
  3. Save, exit, recompile, and run the program.
Step 7: Introduce an Error (Optional)
  1. Modify the code to introduce a small error (e.g., remove the semicolon after System.out.println):
    System.out.println("Hello, World!")
    
  2. Save, exit, and attempt to recompile:
    javac HelloWorld.java
    
  3. Observe the error message. Then, fix the code by adding the missing semicolon and recompile the program.
Step 8: Rename the Class (Advanced)
  1. Reopen the HelloWorld.java file:
    nano HelloWorld.java
    
  2. Change the class name from HelloWorld to HelloJava and update the filename accordingly:
    public class HelloJava {
        public static void main(String[] args) {
            System.out.println("Hello, World!");
        }
    }
    
  3. Save and exit the file, then rename the file itself using the mv command:
    mv HelloWorld.java HelloJava.java
    
  4. Compile the updated file:
    javac HelloJava.java
    
  5. Run the program using the updated class name:
    java HelloJava
    
Step 9: Cleanup (Optional)
  1. Once you have successfully run the program, clean up the directory by removing the .class files:
    rm *.class
    

Summary

In this lab, you learned how to create, edit, compile, and run a simple Java program in a Linux environment using Nano. You practiced using basic Java syntax, including the structure of a class and the main method, which is the entry point for all Java applications. You also explored modifying code, working with comments, handling errors, and renaming classes, all of which are essential steps in developing more complex Java programs.