Difference Between .Java and .Class Files in Java

Shikha Chaudhary Oct 26, 2023
  1. Basics of Writing a Program in Java
  2. Introduction to .java and .class Files
  3. the Difference Between .java and .class Files in Java
  4. Conclusion
Difference Between .Java and .Class Files in Java

Java, a powerful and versatile programming language, is known for its “write once, run anywhere” philosophy.

When working with Java, you encounter two essential file types: .java and .class. These files play distinct roles in the Java development process, representing the source code and compiled bytecode, respectively.

Java is a platform-independent programming language that involves a two-step execution process instead of the usual one-step compilation like the C language.

The first step takes place using a compiler, OS independent. The second step takes place with the help of a virtual machine, also called JVM or Java Virtual Machine.

During the two-step execution, files with the extension .java and .class form. In this article, we’ll delve into the intricacies of both file types, examining their purpose, structure, the significance they hold in the Java ecosystem, and the basic difference between them.

Basics of Writing a Program in Java

To understand the concept of .java and .class files, one must know how a Java program runs. Let us look at the following code as an example.

public class DemoProgram {
  /* This is a demo Java program that will print 'Hello Java' as the output */

  public static void main(String[] args) {
    System.out.println("Hello Java");
  }
}

Follow these steps to run this program.

  • Write the above code in a text editor. Then, save the file as DemoProgram.java.

    Note that the file name should be the same as the name of the class.

  • Open the command prompt window with the path to the directory where you saved this file.
  • Type this command - javac DemoProgram.java. Press the Enter key.
    C :\Users\PC > javac DemoProgram.java
    

    The javac command compiles a Java program. It takes a .java file as input and changes it to a bytecode file.

    This step starts the compilation of code. The cursor moves to the next line once the compilation is successful without errors.

  • Then, type this command - java DemoProgram.
    C :\Users\PC > java DemoProgram
    

    We do not use .java with the file name here. The reason is that the java command takes byte code as input and gives the final output as per the source code.

    Simply put, this time, DemoProgram is not a Java file but a bytecode.

  • The above command makes the program run, and you can see the output as:
    Hello Java
    

Before moving further, note the following points:

  • Source code is the code that a programmer writes in any programming language. We write it manually to solve any problem.

  • In Java, each program constitutes a class. There can be more than one class in a single program, depending on the purpose of writing the code. Like in the example above, there is only a single class called DemoProgram.

We make a text file in the section above and save it with the extension .java. This is the source file that passes through the compiler.

This encodes the source code into an encoding that is machine-independent. This is called Bytecode.

As mentioned already, one program may have one or more classes. Therefore, during encoding, the content of each class goes separately in a different .class file.

These files can then run on any system as they are machine or OS-independent.

Did you see when the .java and .class files come into the picture?

The .java file forms when we save a source code with this extension. The .class file forms when compilation takes place.

Introduction to .java and .class Files

the .java File in Java

.java files are the source code files in Java. They contain human-readable code written in the Java programming language.

These files are the foundation of any Java application, housing the instructions that define the program’s behavior.

We can use a simple text editor or any IDE to write a Java program. Whatever we use, we save the program or the source code that we have written.

To save a Java source code file, we use the .java extension.

Every language has a different convention of saving the source code file. Like in the C language, the source code file has the .c extension; in Python, it is .py, and in C++, we use .cpp.

the .class File in Java

On the other hand, .class files are the compiled bytecode. They are generated from the corresponding .java files during the compilation process.

Unlike .java files, .class files are not human-readable. They contain instructions that the Java Virtual Machine (JVM) can interpret and execute.

When we mentioned that Java is a platform-independent language, it is because of these class files. These files can run on any OS or machine.

A .class file can be considered a compiled .java file. It is the file that runs on the JVM (Java Virtual Machine).

The number of .class files need not necessarily be one. It is equal to the number of classes a source code has, as each class goes into a different .class file after compilation.

Example 1:

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

This is a demo Java program that will print 'Hello Java' as the output.

Output:

C:\Users\PC>javac DemoProgram.java

C:\Users\PC>java DemoProgram
Hello Java

In this code, there is only one class, DemoProgram. So, we get only one .class file, DemoProgram, due to the compilation.

Now, let us look at another example. Below is a Java program with multiple classes, and each class prints a different output.

Example 2: Example.java

// A class
class TestCase {
  public static void main(String[] args) {
    System.out.println("An example with multiple classes.");
  }
}

// Another class
class Demo {
  public static void main(String[] args) {
    System.out.println("This is an example.");
  }
}

// One more class
class Example {
  public static void main(String[] args) {
    System.out.println("It contains three classes.");
  }
}

Compile the file:

C :\Users\PC > javac Example.java

After compilation, we get not one but three .class files as the output. Hence, you can see that there are as many .class files as the number of classes in the source code.

We can run each .class file separately as follows:

C :\Users\PC > java TestCase
An example with multiple classes.
C :\Users\PC > java Demo
This is an example.
C :\Users\PC > java Example
It contains three classes.

Compilation Process: From .java to .class

The process of converting .java files to .class files is known as compilation. This is accomplished using a Java compiler (commonly javac).

The compiler checks the syntax and semantics of the source code, and if no errors are found, it generates the corresponding .class files.

Execution: Running Java Programs

To run a Java program, you need both the .java source files and the compiled .class files.

The JVM is responsible for executing the program. It loads the required .class files, interprets the bytecode, and produces the desired output.

Debugging and Maintenance

When debugging Java applications, developers primarily work with .java files.

The source code provides valuable information for identifying and resolving issues. Debuggers allow for step-by-step execution and inspection of variables in the source code.

Maintenance tasks, such as adding features or fixing bugs, are carried out on the .java files. Once changes are made, the code is recompiled to update the corresponding .class files.

the Difference Between .java and .class Files in Java

The difference between these two types of files is summarized in the following table.

.java File .class File
It contains the Java Source Code. It contains the Java Byte code.
A programmer or an individual writes it. It forms as a result of the compilation of a .java file.
It is human-readable. It is used in JVM for the execution of the program.
It forms before a .class file. Without this, a class file cannot be formed. It always forms after a .java file.
We can write this on any text editor or Java IDE. We can run this on any OS or machine.

the .java Files: The Source Code

Syntax and Structure:

.java files adhere to the syntax rules defined by the Java language. They consist of classes, interfaces, methods, and other constructs. Java source files are organized in a structured manner, with classes and methods encapsulating functionality.

Role in Java Development:

.java files are where developers write, edit, and maintain their code. These files serve as the blueprint for the program’s logic. They are essential for readability, maintainability, and collaboration among developers.

the .class Files: The Compiled Bytecode

Structure and Contents:

Compiled .class files are in a binary format known as Java bytecode. This format is designed to be executed by the Java Virtual Machine (JVM). Each .class file contains a series of instructions, known as bytecode instructions, that the JVM interprets.

The Java Virtual Machine (JVM):

The JVM is a crucial component of the Java platform. It acts as an execution environment for Java applications, interpreting bytecode instructions and translating them into native instructions that the underlying hardware can execute.

Conclusion

In the realm of Java development, understanding the distinction between .java and .class files is paramount. The former serves as the canvas where developers craft their code, written in the familiar syntax of the Java language.

These human-readable .java files are the cornerstone of any Java application, embodying the logic and functionality.

On the other hand, .class files emerge as a result of the compilation process, containing bytecode that the Java Virtual Machine (JVM) can interpret and execute. This binary format ensures Java’s famed platform independence, enabling applications to run on any system with a compatible JVM.

Together, .java and .class files form the backbone of Java’s “write once, run anywhere” philosophy. The former captures the developer’s intent, while the latter provides the means for execution across diverse environments.

Armed with this knowledge, Java developers can navigate the intricacies of the language, crafting robust and portable applications that span the technological landscape.

Related Article - Java File

Related Article - Java Class