Courses / Complete Java Course by codesofrohan / Java Basics / Write Your First Java Program

Write Your First Java Program

Write Your First Java Program

Congratulations! After installing the Java Development Kit (JDK) and Visual Studio Code, you're ready to write your very first Java program. This is an exciting milestone because you'll finally see how Java code is written, compiled, and executed.

In this lesson, we'll create a simple Java program, compile it using the Java compiler, and run it using the Java Virtual Machine (JVM).


Before You Start

Before writing any Java code, make sure Java is installed correctly.

Open PowerShell or Command Prompt and type:

java --version

If Java is installed properly, you'll see the installed version.

Now verify the Java compiler:

javac --version

If both commands work successfully, your system is ready.

If the commands are not recognized, make sure the JDK's bin folder has been added to the PATH Environment Variable.


Creating Your First Java File

Create a new folder anywhere on your computer.

Open the folder using Visual Studio Code.

Now create a new file named:

Main.java

Notice that the first letter of Main is capitalized.

This is important because Java follows naming conventions, and the filename must match the public class name.


Writing Your First Java Program

Type the following code inside Main.java.

public class Main {

    public static void main(String[] args) {

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

    }

}

Save the file.

Congratulations!

You have written your first Java program.


Compiling the Program

Java programs cannot run directly.

They first need to be compiled into Bytecode.

Open the integrated terminal in VS Code and type:

javac Main.java

If there are no errors, Java creates a new file named:

Main.class

This file contains Java Bytecode.


Running the Program

Now execute the compiled program.

java Main

Output:

Hello World

You have successfully executed your first Java application.


What Happened Behind the Scenes?

Although it looked like only two commands, several things happened internally.

Main.java
      │
      ▼
 javac Compiler
      │
      ▼
 Main.class
(Bytecode)
      │
      ▼
 JVM
      │
      ▼
 Machine Code
      │
      ▼
 Output

The Java compiler converted your source code into Bytecode.

The JVM translated the Bytecode into machine code.

Finally, your processor executed it and displayed the output.


Real-World Example

Imagine writing a letter in English.

Your friend understands only Japanese.

Before they can read it, the letter must be translated.

Similarly,

  • You write Java code.

  • The compiler translates it into Bytecode.

  • The JVM translates Bytecode into machine language.

  • The computer finally understands and executes your instructions.


Common Beginner Mistakes

While writing your first Java program, avoid these mistakes:

  • Naming the file main.java instead of Main.java.

  • Running java Main.java instead of java Main.

  • Forgetting to compile before running.

  • Saving the file with the wrong extension.

  • Typing the class name incorrectly.


Key Takeaways

  • Every Java program is written inside a .java file.

  • Compile Java programs using javac.

  • Running javac creates a .class file.

  • Execute Java programs using the java command.

  • The JVM runs Bytecode and displays the output.

  • Java programs must be compiled before execution.


Conclusion

Writing your first Java program is the first practical step toward becoming a Java developer. In this lesson, you learned how to create a Java file, compile it into Bytecode, and execute it using the JVM. In the next lesson, we'll understand the structure of a Java program and learn the purpose of each component.

🔖 Bookmark saved successfully!