You are currently viewing First Java Program – 7 Key Concepts Every Beginner Must Know

First Java Program – 7 Key Concepts Every Beginner Must Know

Leave a Reply

First Java Program is where every Java developer’s journey begins. This simple program is more than just a “Hello World” example — it’s your introduction to how Java works, how code is structured, how it runs, and why certain rules exist. In this post, I’ll share what I learned while writing and exploring my first Java program, including concepts like public static void main, why class names and file names must match, how the Java compiler and JVM work, and even a glimpse into JShell for quick experimentation.


1. Understanding the Structure of the First Java Program

Let’s take the most basic Java program:

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

At first glance, it looks simple. But every keyword here has a reason.

  • public class HelloWorld
    • class is a blueprint for objects in Java.
    • The class name must match the file name (HelloWorld.java) if it’s declared public.
      This is because the compiler and JVM need a predictable entry point when running the program.
  • public static void main(String[] args)
    • public → Accessible from anywhere. The JVM must call this method from outside the class.
    • static → Belongs to the class, not an object. No object creation is needed for execution.
    • void → The method doesn’t return any value.
    • main → The name recognized by the JVM as the program entry point.
    • String[] args → Command-line arguments, if any, are passed here.
  • System.out.println()
    • Prints text to the console.
    • System is a predefined class from the java.lang package.
    • No need to import java.lang — the compiler automatically imports it.

2. Why No Header Files or Package Imports?

In languages like C/C++, we include header files. Java doesn’t need that because:

  • The Java compiler automatically imports java.lang — this package contains essential classes like System, String, and Math.
  • No explicit import is needed for these core classes.
  • This makes Java cleaner and reduces boilerplate code.

If you create your own packages, you’ll need to import them explicitly. But for basic programs, the default imports are enough.


3. Understanding Java Compilation and Execution Flow

I learned that writing code is just the first step. Here’s what happens when you compile and run:

  1. Write the code → Save as HelloWorld.java
  2. Compile → Run javac HelloWorld.java This creates a bytecode file: HelloWorld.class
  3. Run the programjava HelloWorld The JVM (Java Virtual Machine) reads the bytecode and executes it.

4. How JVM Converts Bytecode to Machine Code

The JVM is the magic behind Java’s “Write Once, Run Anywhere” nature.

  • Bytecode is platform-independent, meaning it works on any OS with a JVM.
  • When you run java HelloWorld, the JVM:
    1. Loads the .class file.
    2. Uses the Class Loader to load classes into memory.
    3. Passes the bytecode to the Execution Engine.
    4. The JIT Compiler (Just-In-Time) converts frequently used bytecode into machine code for faster performance.
    5. The CPU executes the machine code.

This process allows the same .class file to run on Windows, Linux, or macOS without changes.


5. Can We Have More Than One Class in a Java File?

Yes, but with rules:

  • You can have multiple classes in one .java file.
  • Only one class can be public.
  • The file name must match the public class name.
  • Other classes (non-public) can be accessed if they are in the same package.

Example:


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

class SecondClass {
    void showMessage() {
        System.out.println("Hello from SecondClass!");
    }
}

First Java Program

6. Introduction to JShell for Quick Testing

Java 9 introduced JShell, a tool for running Java code snippets without creating full files.

To use JShell:

  1. Open your terminal and type: jshell
  2. You can directly type: System.out.println("Hello from JShell");
  3. No need for class or main — perfect for quick testing and learning.

JShell is especially useful for experimenting with syntax and small logic before integrating into a full program.


7. Practice Problems to Strengthen Understanding

Here are a few exercises I practiced:

  1. Write a program to print Name, age, and hobby on separate lines
  2. Write a Java Program to Print the following Pattern
    **
    *
    **
    *

8. Key Takeaways

From my first Java program, I learned:

  • How Java program structure works.
  • The importance of public static void main.
  • Why class name and file name must match for public classes.
  • That Java automatically imports java.lang, so no header files are needed.
  • The flow from source code → bytecode → machine code via the JVM.
  • How multiple classes can exist in one file.
  • That JShell makes learning Java faster.

Final Words

Writing your first Java program is not just about printing “Hello, World!” — it’s about understanding how Java is built, how the JVM runs your code, and the rules that keep everything organized. Once you understand the structure, compilation flow, and tools like JShell, you’re ready to explore more advanced concepts confidently.


Alright — I’ll enhance your blog section by adding a “My Notes” part, plus external and internal links that are SEO-friendly.
Here’s how it can look in your post:


My Handwritten Notes

While learning my First Java Program, I realized that understanding each keyword in the code is essential. At first, public static void main seemed intimidating, but breaking it down into public (access level), static (no object needed), void (no return value), and main (entry point) made it much clearer


Further Reading


You May Also Like

If you’re enjoying this beginner-friendly Java series, here are a few of my other helpful posts that you might find useful: