Jumat, 25 April 2014

Class file in Java - things to know for programmer and developer




.class file in Java is core of achieving platform independence which is key feature of Java. class files are compiled form of any Java program written in byte codes. Since Java program executes inside JVM, these instructions are read by JVM and than translated into machine code for underlying platform. effectively you can run class file on any platform if you have a corresponding JVM supported for that platform. Java compilers are responsible for generating .class file and they follow class file format specified by Sun and its different based on different Java version. 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 interface in Java and 10 Object oriented design principles for Java programmer. If you haven’t got chance to read them you may find them useful.




What is class file in Java



basics of .class file format in JavaIn general every class file in java contains definition of a single class or interface in 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. class file is generated by Java compilers and executed by Java virtual machine and its byte code is verified during class loading process, which address possibility of altering .class file externally. One of the reason Java is considered relatively safe and secure language is its ability to verify class file byte codes before executing it. class file contains 10 basic sections which specifies all data and meta data like magic numbers, major and minor version of class file etc. Read officialVM spec for class file format for more details on how a .class file looks like.





Important points about 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 JDK installation and can be found in JAVA_HOME/bin directory.





2. class file contains byte codes. byte codes are special platform independent instruction for Java virtual machine. 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 instruction for doing same thing, its left on JVM to translate bytecode into machine instruction and by this way java achieves platform independence.





3. class file of inner class contains $ in there name. So if a Java source file contains two classes, one of them is inner class than java compiler will generate two class file. separate class file for top level and inner class. you can distinguish them by looking at there name. Suppose you have top level class as "Hello" and 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 its changed to support new feature introduced in Java 1.5. In general every java compiler and JRE comes with 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 some one 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 class is static or abstract


This class - name of this class


Super class - name of super class


Interfaces -


Fields


Methods


Attributes





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





10. You can view class file 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. As I said this is not a detailed article on .class file format rather this is the bare minimum information regarding .class file an average java programmer should be familiar with. I highly recommend to read more about .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










Reference

























Source:http://javarevisited.blogspot.com/2012/05/10-points-about-class-file-in-java.html

Tidak ada komentar:

Posting Komentar