directory

  • Commands related to VMS in this document
  • The background and importance of Class files
    • Q: Can only Java compiled class files run in the JVM?
    • Class file status
  • Class file structure design analysis
    • 【 QUESTION 】 How would you design a class file?

Commands related to VMS in this document

  1. Writing Java source code
Java source code
  1. Compile the instruction javac xxx. Java to get the.class file
  2. Hexdump -c filename Displays binary files
.class binary file
  1. After compilation, use the javap -c class name to obtain the VM instructions corresponding to the. Class file
Class file corresponding to the VM instruction table

Here’s a quick look at.class files and virtual machine commands that won’t make you feel like you’re in love with them.

The background and importance of Class files

Q: Can only Java compiled class files run in the JVM?

First of all, are only Java compiled class files allowed to run in the JVM? That is, can other languages use the Java VIRTUAL machine as a medium? The answer is: no, it can; Any compiled file can run as long as it complies with JVM file structure specifications, such as Scala. Scala ending files can be compiled as.class files, which can run in the JVM.

Languages have a love-hate relationship with the JVM

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

Class file status

  • Source: the Source code
  • Class: bytecode
  • The Runtime: Runtime

The bytecode form is loaded by the Classloader into runtime form (in memory).

Java’s nascent tagline, “Write once, run anywhere,” was an implementation that satisfied developers’ desire to break through the platform’s limitations, and it soon was. However, when the designers first published the specification, they deliberately split the Java specification into the Java Language Specification and the Java Virtual Machine Specification. That is to say, their ambition for the virtual machine went beyond the Java language. Languages like jRuby can run on top of Java virtual machines. As the platforms (Windows, Linux, Unix) remain stable and the language changes with each passing day, JAVA’s “language-independent” advantage may outweigh the “platform-independent” advantage in the future!

The key to language independence is the virtual machine and bytecode storage format. The virtual machine provides you with an interface, you just need to follow the specified format of the class file to it, it can run your code, so the key is the class file. The virtual machine doesn’t care what language your project is coded in. In the end, all you need to do is compile a class file in the desired class format and pass it to the virtual machine. Am I making myself clear, buddy?

Class file structure design analysis

A class file is a set of 8-bit binary streams without any delimiters. Because there are no delimiters, the order and number of items in a class file are strictly limited. The meaning, length, and order of each byte are not allowed to change. See the diagram for the specific class file content.

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 below:

.class binary file
Contents of the class file

【 QUESTION 】 How would you design a class file?

Take the Java language for example. If you were a designer and you compiled your Java source code into a class file, how would you design that class file?

Ideas: Convert to easy to understand implementation, know Java, generally know XML files, a JavaBean can be converted to use XML this descriptive language to express, the reason is that XML is specified protocol, your Java to become XML will follow my protocol to do things, using XML to describe a student object as follows:

Java objects represented by XML

Your class is a hexadecimal file, so you have to create your own protocol so that your hexadecimal descriptions can be translated into instructions that the JVM can understand. Your “xx” letters tell the virtual machine what to do. The “oo” letters tell the virtual machine what to do, and the “xxoo” letters tell the virtual machine what to do. Here’s a look at the actual JVM instructions and how they correspond to the class file (there are currently about 255 JVM instructions).

The JVM instruction table

Convert JAVA source code to a CLASS file, and see how to convert it:

  1. A Java class corresponds to a class file (possibly more than one, if the class has inner classes). What does it contain? Definitely not to lose things, this is the most basic requirement, if someone clearly wrote “you green me”, you make “you green me”, then the mood is quite ups and downs.
  2. How do you parse and store things in Java classes?
  • A class file may be a Java class or an interface. A class may represent more than one class or interface. Come on, type on the board, bold design is a set of interface types, a set of common classes, more than one? I’m going to add two fields, number of interfaces, number of classes, everything is perfect, look in the class file, I can find it, OK, I’m a genius, next.
  • Java has properties, methods, constants, fields, how to break? Go ahead, set up, store, and end up with the table structure class file shown above.

Let’s review the class file again:

  1. What’s in a Java class, and what should a class file store?
  • Two data types: unsigned numbers and tablesUnsigned numberUnsigned numbers of 1 byte, 2 byte,4 byte, 8 byte, such as U1, U2,u4,u8, and so on, can be used to describe numbers, index references, quantity values, or string values.tableJust like object reference types in Java classes, object attributes can be primitive data types (corresponding to U1,U2 unsigned number) or other objects (corresponding to other tables). Parameter entities in Java projects usually end with “_Param” (tables in class files usually end with “_info”).
  • The order in the figure above is exactly the order required by the Class file
  • Each counter is used to describe the number of data in the table. For example, the method counter is methods_count, which means methods_count in the method-info table
  • To reorganize, the table structure is expressed in C language like the following pseudocode (ignore the red line of IDEA error)
Class file code representation

Is it over? Class file structure and bytecode instructions will be analyzed in the next installment. This is just an appetizer. The next installment will be the main course. In addition, the life cycle of Java classes in the JVM can be seen in my public account