Difference Between .java and .class

Shikha Chaudhary Jan 30, 2023 Dec 15, 2021
  1. Basics of Writing a Program in Java
  2. the .java File in Java
  3. the .class File in Java
  4. the Differences Between .java and .class Files
  5. Conclusion
Difference Between .java and .class

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

The first step execution takes place using a compiler, OS independent. The second step execution 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. This article will look at the basic difference between .java and .class files.

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. 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 onto the next line once the compilation is successful without errors.
    C:\Users\PC>javac DemoProgram.java
    
  • Then, type this command - java DemoJava. 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.
    C:\Users\PC>java DemoProgram
    
  • 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.

Then 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.

the .java File in Java

The .java file is nothing but the source code itself in Java.

Simply put, it is the file containing the code we write. 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

The .class file is the file that contains the byte code of a class in the source code. This file is formed when compilation takes place.

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{
     
       /* 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"); 
       }
}

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.

Example 2:

//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.

the Differences Between .java and .class Files

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.

Conclusion

This article taught us the primary difference between a .java file and a .class file. We also saw how a Java program runs on the machine level and the user level.

This concept is at the base of Java Programming, and it is important to understand it to run Java Programs and applications efficiently.

Related Article - Java File

Related Article - Java Class