In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is a JAVA Class file". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is a JAVA Class file".
I. language independence of JVM
What is the use of JVM?
Running java, ah, is it difficult to run python?
This is true, but it is incomplete. JVM does not just run java programs.
In fact, what runs on JVM itself is not a java file, but a class file.
Java is not the only one that can compile and convert into class files.
This is the language independence of JVM.
Whether you can run python or not depends on whether there is a tool that can convert python to class files.
Of course, this doesn't make much sense. After all, python also has its operating environment, and in a sense, it is more powerful than java, and the core class library is more complete.
Various languages also have their own platforms, so there is no need to force compilation.
But it makes sense to master class files.
As a programmer, do you have or have you ever had the extravagant hope of creating a language? It is best to develop in Chinese.
But reality, or some mentor in college, gives you cold water.
First take three or five years to study the compilation, and then consider the implementation of these.
For three or five years, cauliflower is getting cold.
Now, with JVM, there seems to be a glimmer of hope.
II. The nature of class files
To achieve the previous vision, that is to say, to develop a compilation tool. The first thing to do is to deconstruct the class file itself.
In any case, the essence of an class file is a set of binary streams based on 8-bit bytes.
Remember, it's binary.
To prove this, we still need to use some tools. For example, Sublime.
It is not a tool to view binary directly, but a hexadecimal editor (binary and hexadecimal can be switched seamlessly).
There seems to be something about python in this. When in use, just click on the sublime_text.exe file.
Then select the class file and open it, as shown in the following figure.
Is the person looking at it dazzled? What is this?!
As I said earlier, binary, no, this is hexadecimal.
If you don't want to look at hexadecimal, you can also use javap to check the bytecode instructions (for details, see "how a piece of java code is executed" above).
If you don't want to open the command line, there is a tool called jclasslib that provides a graphical interface and plug-ins for idea.
But it's not the point, so ignore it for the time being.
III. Revealing the structure of class files
There are only two data types in the class file format, unsigned numbers and tables.
Among them, the unsigned number contains all the basic data types and strings, index references, etc., according to the byte length, it can be divided into u1memery u2jinu4recoveru8, which represents the length of the unsigned number respectively.
The table, that is, the object type.
Next, take the parsed content of the sublime file as the model, and talk about the composition of the class file in order.
(1) the first four bytes of a class file are called magic numbers, and its function is to determine whether the file is a Class file that can be accepted by the virtual machine.
For example, the value of the magic number in the above is:
It represents that the file is a class type file, believe it or not, you can open a few more class files to have a look.
(2) the next four bytes represent the version of jdk
The content above represents that the version of jdk is 1.8.
PS:jdk1.1 version number is 45, after each major version, the number + 1, so the jdk1.8 version number is 51 (decimal), converted to hexadecimal is 34.
(3) the following concept is constant pool.
The above is the counter of the constant pool, through which we calculate that the number of constants is 15 (the calculated number minus 1, because the starting number of the counter is not 0, but 1).
We use the javap command to open the constant pool and prove that there are indeed 15 constants.
(4) after the constant pool is the access flag, which is mainly divided into the following categories
Let's go back and take a look at the source code of this class (it's so simple)
Java code
Public class ByteCode {public ByteCode () {}}
This class is non-interface, non-abstract, non-enumerated, non-system code, non-final, has pulbic, and the compiler is after jdk1.2, so the flag that meets the condition is:
ACC_PUBLIC and ACC_SUPER, with 0001 and 0020 corresponding flags, add up to 0021. The location is as follows:
(5) Class index, parent index and interface index
The access flag above is followed by a class index with an index value of 0002, which corresponds to the second place in the constant pool.
The class index is followed by the husband index, with an index value of 0003, corresponding to the third place in the constant pool.
The parent index is followed by the interface index, with an index value of 0000, indicating that the class does not implement any interfaces.
(6) Field table, method table, property table
After the three indexes is the field table.
The field table is 0000, which means no fields.
As shown in the above figure, the method table is divided into four parts
The result of the method table counter is 1, which means there is a field
The method table access flag is 0001, which represents public
The method table is named index 0004, corresponding to the fourth constant pool.
The method table description index is 0005, corresponding to the fifth constant pool.
Property sheets, and so on.
four。 Bytecode instruction
A separate chapter on bytecode instructions, which exists in the method table, is classified as follows:
(1) load and store instructions
For this section, see "how a piece of java code is executed")
(2) operational or arithmetic instructions
Source code:
Java code
Public class Test {public void add (int a pencil int b) {System.out.println (aqimb); System.out.println (amelb); System.out.println (aquib); System.out.println (aplink b);}}
The bytecode instructions are as follows:
(3) Type conversion instruction
Source code:
Java code
Public class Test {public void add (int a line int b) {int c = 1; long d = c;}}
Bytecode instruction:
(4) create instance instruction
Needless to say, it's new.
(5) create array instructions
Source code:
Java code
Public class Test {public void add (int a line int b) {int [] c = new int [4]; String [] d = new String [5];}}
Bytecode instruction:
(6) access field instruction
Source code:
Java code
Public class Test {private static String name = "1"; private String age = "2"; public static void main (String [] args) {Test test = new Test (); String a = test.age; String b = Test.name;}}
Bytecode instruction:
(7) Array access instruction
Source code:
Java code
Public static void main (String [] args) {String [] a = new String [5]; a [1] = "2"; String b = a [1];}
Bytecode instruction:
(8) check instance type directives
That's instanceof, the demo.
(9) method returns instruction
That's return, the demo.
five。 Abnormal operation
Look directly at a piece of code:
Java code
Public class Test {public void test () {InputStream in = null; try {in = new FileInputStream ("i.txt");} catch (FileNotFoundException e) {e.printStackTrace ();} finally {try {in.close ();} catch (IOException e) {e.printStackTrace () }
The code is a typical file stream operation, and unlike other code, it catches two exceptions.
So how does the bytecode instruction handle the exception?
We can see that there is an exception table at the bottom, that is, the exception table, which records all the abnormal data.
Take, for example, the first row of the exception table, where from,to represents, respectively, if an exception occurs between lines 12 and 16, skip directly to line 19 (target).
six。 Packing and unpacking
This is a topic that cannot be avoided.
Anyone with a little bit of java foundation knows that java has eight basic data types, each of which corresponds to a wrapper class. Such as int to Integer,long to Long.
In general, both the underlying data type and the wrapper class can assign values to each other. But what is the logic of this?
Java code
Public class Test {public static void main (String [] args) {Integer I = 1; int a = 2; int b = 3; I = a; b = I;}}
Let's take a look at bytecode instructions.
From the bytecode instruction, we can see that there are three disassembly operations.
For the first time, call the valueOf method of Integer, and convert the constant 1 to Integer.
The second time, call the valueOf method of Integer, and change the stack top value 2 to Integer type
The third time, call the intValue method, say Integer to int, and then assign a value to b.
The first two parts are boxed, and the second step is unpacking.
This is the underlying implementation logic of unpacking.
Thank you for your reading, the above is the content of "what is the JAVA Class file", after the study of this article, I believe you have a deeper understanding of what is the JAVA Class file, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.