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 structure of .class files and the status of .class files?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you the structure of the .class file and the status of the .class file. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Related virtual machine commands write Java source code Java source code compilation instructions javac xxx.java, get .class file hexdump-C filename can view binary files. Class binary files are compiled using javap-c class name to get virtual machine instruction table corresponding to virtual machine instruction class file corresponding to .class file.

First of all, let's take a look at the .class files and virtual machine instructions, which won't make you feel like you love you across the sea.

Background and importance of Class files [question] can only class files compiled in Java language run in JVM?

First of all, the question is whether only class files compiled in Java can be run in jvm, that is, can other languages use the Java virtual machine as a medium? The answer is: no, yes; as long as it conforms to the specification of JVM file structure, the compiled file can be run, such as the end file of Scala language .Scala, it can be compiled into .class file and can be run in jvm. Ha, it's so stiff here.

Various languages and the love and hatred of JVM

As shown in the love and hate between various languages and JVM, as long as your language goes through its own compiler and finally gets a .class file that JVM needs, then you can.

Status of class file Source: source code Class: bytecode Runtime: runtime

Bytecode form is changed to run-time form (in memory) by Classloader loading.

Java's nascent slogan "write once, run everywhere" is an implementation that satisfies developers' desire to break through platform restrictions, which will be realized soon. However, when the designers first released the specification document, they deliberately decomposed the Java specification into "Java language specification" and "Java virtual machine specification", that is to say, their ambitions for virtual machines are not limited to Java, their future goal is to break through language restrictions, and now they have realized that Scala,jRuby can run on Java virtual machines. As the platform (windows,linux,unix) is basically stable and the language is changing with each passing day, the advantage of "language-independent" of JAVA may outweigh that of "platform-independent" in the future!

The key to achieving language independence is the virtual machine and bytecode storage format. It can be understood that the virtual machine provides you with an interface, you just need to send him the class file in the format specified by him, and he can run your code, so the key is this class file. The virtual machine doesn't care what language your project uses for code implementation. In the end, you just need to compile the class file according to the class format he wants and send it to him, and you can run on the virtual machine successfully. Did I make myself clear, man?

Analysis on the structure Design of Class Class File

Class file is a group of binary streams based on 8-bit bytes, without any delimiters in the middle. It is precisely because there is no delimiter that the data items in the class file are strictly limited in order and quantity, and the meaning, length and order of each byte are not allowed to change. See the picture for the specific contents of the class file.

The class file uses a pseudo structure similar to the C language structure to store data, which is essentially a table, as shown in the figure:

.class binary file

Class file corresponding to the content [question] how would you design class file?

First of all, throw out a question, JVM is to serve Java from the beginning, take the Java language as an example, if you are a designer and you compile your Java source code into a class file, how would you design this class file?

Idea: convert it into an easy-to-understand implementation. Those who know Java generally know xml files. A JavaBean can be transformed into a descriptive language like xml. The reason is that xml specifies a protocol. If you want to turn your Java into a xml, you have to follow my agreement. Describe a student object with xml as follows:

Java object represented by xml

In fact, if the protocol is very understandable, your class is a hexadecimal file, so you must develop your own protocol, so that your hexadecimal description can be transformed into virtual machine instructions that JVM can understand, that is, the letters "xx" are to tell the virtual machine what to do, the letters "oo" are to tell the virtual machine what to do, and what does "xxoo" mean? Let's first take a look at the actual JVM instructions and their correspondence with class files (there are now about 255JVM instructions)

JVM instruction sheet

Conversion: it's easy to have an idea. Convert the JAVA source code into a CLASS file to see how to convert it:

A Java class corresponds to a class file (possibly multiple, if there are inner classes in the class), what is in it? Certainly can not lose things, this is the most basic requirement, if people clearly write "you green me", you turn into "you green me", then this mood ups and downs. How do you analyze the things in the Java class and how to design the storage? Simply dissect, the class file may be a class class in Java, or it may be an interface, and there may be more than one class and interface in a class. Come on, knock on the blackboard and draw a key point. Bold design is a collection of interface types, a collection of common classes, and more than one? Add two more fields, the number of interfaces, the number of classes, everything is so perfect, look in the class file, I can find, OK, I am a genius, next. There are attributes, methods, constants and fields in Java. How to break them? Go on, get the collection together, save the number, and finally get the class file of the table structure in the above figure.

Let's review the contents of the class file:

What is in the Java class and what should be stored in the class file? Two data types: unsigned number and table unsigned number belong to the basic data type (there are also basic data types in the Java class). U1, 2, 4, and 8 bytes of unsigned numbers can be used to describe numbers, index references, quantity values, or string values. Tables are just like object reference types in the Java class. Object properties can be basic data types (corresponding to U1Magic U2 unsigned numbers) or other objects (corresponding to other tables). Parameter entities in Java engineering projects usually end with "_ Param" (tables in class files are used to ending with "_ info"). That is, the order strictly required by the Class file is that each counter is mainly used to describe the number of data in the table. For example, the value of the method counter is methods_count, which represents that there is a "methods_count" method in the method table method--info. The table structure is expressed in C language similar to the following pseudo code (ignoring the red line of idea error) the class file code indicates that the above is the .class file structure and what is the status of the .class file. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report