What is .class File and Bytecode in Java?

This tutorial will help you to learn about what is .class file and bytecode in java language. You may like to read previous tutorial about How to Write, Compile and Run Your First Java Program?

What is .class File and Bytecode

  • When Java source file is compiled by Java compiler it is converted into Java class file with .class extension.
  • The Java class file contains Java bytecode (highly optimized set of instructions) which is executed by Java Virtual Machine (JVM).
  • .class file contains symbols and each bytecode instruction is stored into one byte exactly.
  • .class file of each class is separately stored. Its name is same as class name in source file. A Java program can have any number of classes. If below program is compiled then two .class file will be created with name A.class and B.class.

class A
{


public static void main(String…s)


{


System.out.println(“A”);


}


}





class B
{


public static void main(String…s)


{


System.out.println(“B”);


}


}

  • A .class file can be executed on any operating system; we just need JVM of that operating system. This makes Java machine independent.

Difference Between .class File in Java and .exe File in C/C++

.class
.exe
It contain bytecode instructions for Java Virtual Machine (JVM).
It contains machine code instructions for Operating System.
Platform independent.
Platform dependent.
It can be converted into source code.
It can’t be converted into source code.
The .class file can’t be executed directly so viruses can’t be attached
to it and hence secure.
The .exe file can be executed directly so viruses can be attached to
it and hence insecure.

Leave a Reply

Your email address will not be published. Required fields are marked *