Thursday, July 29, 2021

What is Class File and Byte Code in Java? Example

.class file in Java is the core of achieving platform independence which is a key feature of Java. class files are compiled form of any Java program written in byte codes. Since the Java program executes inside JVM, these instructions are read by JVM and then translated into machine code for the underlying platform. effectively you can run the class file on any platform if you have a corresponding JVM supported for that platform. Java compilers are responsible for generating .class files and they follow the class file format specified by Sun and it's different based on different Java versions.

This article is not a detailed one focused on class file format instead it contains basic information about class files and intended to give an idea about what is a class file in Java and what are some important points related to class files.

This Java article is in continuation of my series of 10 points of the interface in Java and 10 Object-oriented design principles for Java programmers. If you haven’t got a chance to read them you may find them useful.



What is the class file in Java?

basics of .class file format in JavaIn general, every class file in java contains a definition of a single class or interface in a stream of 8-bit bytes. Every .class file follows a predefined class file format, failing which .class file will be deemed invalid and not run on any JVM or JRE. the class file is generated by Java compilers and executed by Java virtual machine, and its byte code is verified during the class loading process, which addresses the possibility of altering .class file externally. 

One of the reasons Java is considered a relatively safe and secure language is its ability to verify class file byte codes before executing it. a class file contains 10 basic sections which specify all data and metadata like magic numbers, major and minor versions of a class file, etc. Read official VM spec for a class file format for more details on how a .class file looks like.



Important points about a class file in Java

1. class file in java is generated when you compile .java file using any Java compiler like Sun's javac which comes along with JDK installation and can be found in JAVA_HOME/bin directory.

2. class file contains byte codes. byte codes are special platform-independent instructions for Java virtual machines. bytecode is not a machine language and mere instruction to JVM. since every machine or processor e.g. INTEL or AMD processor may have different instructions for doing the same thing, it's left on JVM to translate bytecode into machine instruction and by this way, java achieves platform independence.

3. class file of the inner class contains $ in their name. So if a Java source file contains two classes, one of them is an inner class then the java compiler will generate two class file. the separate class file for the top-level and inner class. you can distinguish them by looking at their names. Suppose you have top-level class as "Hello" and the inner class as "GoodBye" then java compiler will generate two class file:

Hello.class
Hello$GoodBye.class

Hello$GoodBye.class is a class file for inner class. whose name is in format of top-class$inner-class.

4. You can look bytecode of class file using javap command. javap command can also display method and field information from .class file. see my post how to decompile .class file in Java for more details.

5. class file format is subject to change and it's changed to support new feature introduced in Java 1.5. In general every java compiler and JRE comes with a supported version of .class file format and you can not run a .class file which is in higher version of those supported by JRE. this often result in java.lang.UnsupportedClassVersion. class file has two version major and minor which is included inside class file. See my post Bad version number in .class file for more details.

6. .class file in java is identified by a magic number in header which is a 4 byte CA FE BA BE ( in hex). which is the first element in .class file, followed by major and minor versions of class file

7. .class file allows Java Virtual machine to implement security. Since number of instruction class file contains are limited. also if someone tempers .class file externally it may not run on different browsers and JRE.

8. Java class file can be divided into 10 basic sections e.g.
  • Magic Number : 0xCAFEBABE
  • Major and Minor class version or .class file
  • Constant pool - constants for this class
  • Access modifiers - to indicate whether the class is static or abstract
  • This class - name of this class
  • Super class - name of superclass
  • Interfaces -
  • Fields
  • Methods
  • Attributes

9. Top level .class file name is same as the name of java source file e.g. Hello.java will produce Hello.class

10. You can view class files in any hex editor or any byte code viewer plug-in in Eclipse.

That's all on what is class file and basics of .class file in Java. As I said this is not a detailed article on .class file format rather this is the bare minimum information regarding the .class file an average java programmer should be familiar with. I highly recommend reading more about the .class file format if you are interested but it depends upon your experience level and beginners may not find the class file format easy to understand.


Related tutorials on Java




1 comment :

Unknown said...

could you please give a detailed information about .class file in java.

Post a Comment