Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java Class file?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is a Java Class file". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what is a Java Class file"?

Class file

What is a Class file?

The reason why java can achieve cross-platform is that its compilation phase is not to compile the code directly into the platform-related machine language, but to compile the java bytecode in binary form, put it in the Class file, and then load the Class file by the virtual machine to parse out the content needed for the program to run. Each class is compiled into a separate class file, and the inner class generates its own class as a separate class.

Basic structure

Find any class file and open it with Sublime Text like this:

Do you look confused, but the basic format of class files is given in the java virtual machine specification, and you just need to parse it according to this format:

ClassFile {u4 magic;u2 minor_version;u2 major_version;u2 constant_pool_count;cp_info constant_ pool [constant _ pool_count-1]; U2 access_flags;u2 this_class;u2 super_class;u2 interfaces_count;u2 interfaces [interfaces _ count]; U2 fields_count;field_info fields [fields _ count]; U2 methods_count;method_info methods [methods _ count]; U2 attributes_count;attribute_info attributions [attributes _ count];}

The field types in ClassFile are U1, U2, and U4. What type is this? In fact, it is very simple, which represents 1 byte, 2 bytes and 4 bytes respectively.

The first four bytes are: Magic, which is used to uniquely identify the file format, which is generally called magic number (magic number), so that the virtual machine can recognize whether the loaded file is in class format, and the magic number of class file is cafebabe. Not only class files, but basically most files have magic numbers to identify their own format.

The next part is mainly some information about the class file, such as constant pool, class access flag, parent class, interface information, fields, methods and so on.

Analysis

Field Type

The field types in ClassFile mentioned above are U1, U2, and U4, which represent 1-byte, 2-byte, and 4-byte unsigned integers, respectively. Short, int and long in java are signed integers with 2, 4 and 8 bytes respectively. Removing the symbol bits can just be used to represent U1, U2, and U4.

Public class U1 {public static short read (InputStream inputStream) {byte [] bytes = new byte [1]; try {inputStream.read (bytes);} catch (IOException e) {e.printStackTrace ();} short value = (short) (bytes [0] & 0xFF); return value;} public class U2 {public static int read (InputStream inputStream) {byte [] bytes = new byte [2]; try {inputStream.read (bytes);} catch (IOException e) {e.printStackTrace ();} int num = 0X (int I = 0) I < bytes.length; iTunes +) {num

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report