Lab 01: Hello World & Java Basics

🎯 Objective

Write, compile, and run your first Java program using the command line.

πŸ“š Background

Java is a compiled, statically-typed language. Source code (.java) is compiled by javac into bytecode (.class), then executed by the Java Virtual Machine (JVM) via java. This compile-once, run-anywhere model is Java's core promise.

⏱️ Estimated Time

20 minutes

πŸ“‹ Prerequisites

  • Docker installed

  • Access to innozverse-java:latest Docker image

πŸ› οΈ Tools Used

  • Docker (innozverse-java:latest)

  • javac (Java compiler)

  • java (JVM runtime)

πŸ”¬ Lab Instructions

Step 1: Verify the Java environment

πŸ’‘ You should see the Java version (17+). The JVM is pre-installed in the Docker image.

Step 2: Create your first Java source file

πŸ’‘ In Java, the filename must match the public class name exactly (case-sensitive).

Step 3: Understand the anatomy of a Java program

  • public β€” accessible from anywhere

  • static β€” belongs to the class, not an instance

  • void β€” returns nothing

  • String[] args β€” command-line arguments array

Step 4: Compile the Java source file

πŸ’‘ This produces /tmp/HelloWorld.class β€” the bytecode file executed by the JVM.

Step 5: Verify the class file was created

Step 6: Run the compiled program

πŸ“Έ Verified Output:

Step 7: Pass command-line arguments

πŸ“Έ Verified Output:

Step 8: Compile and run in one command

πŸ’‘ From Java 11+, you can also run single-file programs directly: java HelloWorld.java (no separate compile step needed for simple programs).

βœ… Verification

Run the full verification:

πŸ“Έ Verified Output:

🚨 Common Mistakes

  • Filename mismatch: public class Foo must be in Foo.java β€” not foo.java or FOO.java

  • Missing semicolons: Every statement in Java ends with ;

  • Running without compiling: You must javac before java

  • Wrong classpath: Use -cp /tmp when the .class file is in /tmp

πŸ“ Summary

You've written, compiled, and run your first Java program. Key takeaways:

  • Java source β†’ .java files

  • Compiled bytecode β†’ .class files

  • Run with java ClassName (no .class extension)

  • Every Java program needs a main method as its entry point

πŸ”— Further Reading

Last updated