After installing the JDK and setting up your Java development environment, the next step is to understand how a Java program is created, compiled, and executed.
A Java program normally begins as a source code file with a .java extension. The Java compiler converts this source code into bytecode, which is stored in a .class file. The JVM then loads and executes that bytecode.
The complete process can be summarized as:
Java Source Code
↓
.java file
↓
Java Compiler (javac)
↓
Bytecode
↓
.class file
↓
JVM
↓
Program Output
Understanding this process is important because it explains how Java programs actually work.
Creating a Java File
A Java source file is a text file that contains Java programming code. Java source files normally use the .java extension.
For example:
Hello.java
You can create a Java file using any text editor or code editor, such as:
- Visual Studio Code
- IntelliJ IDEA
- Eclipse
- Notepad
- Another text editor
For your first program, create a file named:
Hello.java
Inside the file, write:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save the file as Hello.java.
Understanding the Basic Structure of a Java Program
Let’s break the program into its important parts.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The first line is:
public class Hello {
This declares a class named Hello.
The class contains the program’s main() method:
public static void main(String[] args) {
The statement inside the method prints text:
System.out.println("Hello, World!");
Finally, the closing braces end the method and the class.
A simplified structure looks like this:
Class
└── main() method
└── Java statements
What Is a Class?
A class is a fundamental building block of Java programs. It defines a structure that can contain fields, methods, constructors, and other members.
In our example:
public class Hello {
Hello is the class name.
When a class is declared as public, the source file name normally needs to match the public class name.
Therefore:
Hello.java
contains:
public class Hello
If you save the file with a different name, such as Test.java, while keeping public class Hello, the compiler will report an error.
What Is the main() Method?
The main() method is the standard entry point used when launching a standalone Java application.
The commonly used form is:
public static void main(String[] args)
Each part has a purpose.
public
public means the method can be accessed by the JVM when starting the application.
static
static means the method belongs to the class rather than requiring an object of the class to be created first.
This allows the JVM to invoke main() without first creating a Hello object.
void
void means the method does not return a value.
main
main is the method name recognized as the standard application entry point.
String[] args
This parameter allows command-line arguments to be passed to the application.
For example:
java Hello Java
The value Java can be received through the args array.
Writing Hello World
The first Java program many beginners write is the Hello World program.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The following statement performs the actual output:
System.out.println("Hello, World!");
System.out represents the standard output stream, and println() prints the supplied text followed by a new line.
The output will be:
Hello, World!
Compiling Java Code
Java source code cannot normally be executed directly by the JVM. First, it must be compiled into Java bytecode.
The Java Development Kit provides the compiler called javac.
Open a terminal in the directory containing Hello.java and run:
javac Hello.java
If there are no compilation errors, the compiler creates a .class file:
Hello.class
The process is:
Hello.java
↓
javac
↓
Hello.class
The .class file contains Java bytecode rather than the original Java source code.
What Is a .java File?
A .java file is a Java source code file.
It contains code written by the developer using the Java programming language.
For example:
Hello.java
might contain:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Developers read, edit, and maintain .java files during application development.
A Java project can contain many .java files:
MyApplication/
├── Main.java
├── Student.java
├── Teacher.java
└── Course.java
Each file can contain one or more classes, although public top-level classes follow Java’s naming and file rules.
What Is a .class File?
A .class file contains Java bytecode generated by the Java compiler.
For example:
Hello.java
is compiled into:
Hello.class
The .class file is not normally intended for humans to read or edit. It contains instructions in the JVM’s bytecode format.
The JVM loads this bytecode and executes it.
.java
↓
Source Code
↓
Compiler
↓
.class
↓
Bytecode
↓
JVM
Running Java Code
After compiling the program, you can run it using the java command.
Run:
java Hello
Notice that you normally do not write:
java Hello.class
Instead, you provide the class name:
java Hello
The JVM locates the compiled class, loads it, finds the appropriate main() method, and begins execution.
The output is:
Hello, World!
How Java Code Executes
Java execution involves several stages.
Suppose you have:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
The process is:
Step 1
Java source code is written
↓
Hello.java
Step 2
The JDK compiler compiles the source
↓
javac Hello.java
Step 3
Bytecode is generated
↓
Hello.class
Step 4
The JVM loads the bytecode
↓
java Hello
Step 5
The JVM executes the bytecode
↓
Hello, World!
This is the basic execution model of a Java application.
Source Code → Bytecode → JVM
One of the most important concepts to remember in Java is:
Source Code → Bytecode → JVM
The source code is written by the programmer:
System.out.println("Hello, World!");
The Java compiler converts the source code into bytecode.
Hello.java
↓
javac
↓
Hello.class
The JVM then executes the bytecode:
Hello.class
↓
JVM
↓
Program Execution
This separation between source code and execution is one of the key characteristics of the Java platform.
Does the JVM Directly Execute Java Source Code?
Normally, no.
The JVM is designed to execute Java bytecode, not ordinary .java source code.
The Java compiler first translates source code into bytecode:
Java Source Code
↓
javac
↓
Bytecode
↓
JVM
This is why the JDK is required when you want to compile Java source code.
What Happens Inside the JVM?
After you run:
java Hello
the JVM performs several operations.
It loads the required class, verifies the bytecode, prepares runtime memory, and begins executing the program.
The JVM’s execution engine can interpret bytecode and use Just-In-Time (JIT) compilation to compile frequently executed code into native machine instructions during runtime.
A simplified view is:
Hello.class
↓
Class Loader
↓
Bytecode Verification
↓
Runtime Memory
↓
Execution Engine
↓
Native Execution
This process happens behind the scenes while the Java application is running.
Java Program Structure
A basic Java program can contain several important elements.
public class Student {
int age = 20;
public void display() {
System.out.println("Student");
}
public static void main(String[] args) {
System.out.println("Java Program");
}
}
Here:
Student
│
├── Field
│ └── age
│
├── Method
│ └── display()
│
└── main()
└── Program execution begins here
As you progress through Java, you will learn how classes can contain fields, constructors, methods, nested classes, interfaces, and other members.
Java Statements
A Java statement represents an instruction that the program can execute.
For example:
int age = 25;
and:
System.out.println(age);
Most Java statements end with a semicolon:
;
For example:
int number = 10;
number = number + 5;
System.out.println(number);
The semicolon tells the compiler where the statement ends.
Java Comments
Comments are used to explain code and are ignored during normal program execution.
A single-line comment begins with //:
// This prints a message
System.out.println("Hello");
A multi-line comment uses /* */:
/*
This is a
multi-line comment.
*/
Comments are useful for documenting code and explaining complex logic.
A Complete Example
Let’s put the basic concepts together.
Create:
Student.java
Write:
public class Student {
public static void main(String[] args) {
String name = "John";
int age = 20;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Compile it:
javac Student.java
This creates:
Student.class
Run it:
java Student
Output:
Name: John
Age: 20
The complete process is:
Student.java
↓
javac
↓
Student.class
↓
JVM
↓
Program Output
.java vs .class
| Feature | .java File | .class File |
|---|---|---|
| Contains | Java source code | Java bytecode |
| Created by | Developer | Java compiler |
| Readable by humans | Yes | Not normally |
| Used for editing | Yes | No |
| Created before compilation | Yes | No |
| Executed by JVM | No | Yes |
| Example | Hello.java | Hello.class |
JDK, Compiler, and JVM Together
It is useful to understand how the components work together.
JDK
│
↓
Java Compiler
javac
│
↓
Java Bytecode
.class
│
↓
JVM
│
↓
Program Execution
The JDK provides the compiler, the compiler produces bytecode, and the JVM executes the bytecode.
This is one of the most important workflows to understand before moving into advanced Java topics.
Common Beginner Mistakes
Using the Wrong File Name
If you write:
public class Hello {
the file should normally be:
Hello.java
Running With .class
Use:
java Hello
rather than:
java Hello.class
Forgetting to Compile
If you only create:
Hello.java
you need to compile it before running it with the standard javac workflow:
javac Hello.java
Then:
java Hello
Missing the main() Method
A standalone Java application needs an appropriate entry point, commonly:
public static void main(String[] args)
Without it, the JVM cannot start the application using the standard launcher convention.
FAQs
What is a .java file?
A .java file contains Java source code written by the developer.
What is a .class file?
A .class file contains Java bytecode produced by the Java compiler.
What command compiles Java code?
The standard Java compiler command is:
javac FileName.java
What command runs a Java class?
Use:
java ClassName
For example:
java Hello
Why does Java use bytecode?
Bytecode provides a common format that compatible JVM implementations can execute on different operating systems and hardware platforms.
What is the purpose of main()?
The main() method is the standard entry point used by the Java launcher to start a standalone Java application.