Talk about Java bytecode

Source: Internet
Author: User

Bashan Chu Water desolate, 23 years abandoned in place.
Nostalgic empty Yin heard flute fu, to the township turned like rotten ke people.
Sinking boat side thousand sails, the disease tree before the million wood spring.
Listen to June song today, a cup of wine long spirit.

First, what is Java bytecode?

Borrowing algorithm (4th) excerpt: It is a low-level representation of a program that can run on a Java virtual machine. Abstracting a program into bytecode guarantees that Java programs can run on a variety of devices. OK, this answer allows us to go back to the execution of the program to see the next question:

Second, how is the procedure implemented?

Students who have studied computer should be disgusted with a course-the principle of compiling, the main purpose of this course is to explain to you the execution of the code that you see written. We usually use the C + +, Java, Python and so on are the advanced computer language, but the computer can only understand "0" and "1" is the binary machine code, so the high-level program language to perform on the machine need to translate themselves into two-level machine code, the process of translation is mainly by the compiler to complete , such as C + + programs compiled on the Windows platform can form an. exe executable file, and on UNIX or Linux platforms will be compiled to form a. dll file, the C + + compiler for different platforms is different.

Third, how is the Java program executed?

Java is a bit more complicated, because Java has a cross-platform feature, in order to implement this feature in Java execution on a virtual machine, this virtual machine that is jvm,java through the JVM to shield the differences between different platforms, so that once compiled everywhere execution. The JVM is between the Java compiler and the OS platform, and the Java compiler only needs to target the JVM to generate code that the JVM understands, which is the bytecode that the JVM translates into binary machine code that the real machine understands.

How is the bytecode generated?

The programs we write are in. java format, which is usually done by clicking on Eclipse's Run key to see the results in the console, but it can be even cooler, and if you have a JDK, you can compile and run your. java file directly on the command line, which will form a. class file, which The. class file is a byte code.

For example, you can create a new Helloworld.java file with text:

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        System.out.println ("Hello Java");}    }

Open the command line into the file directory, enter the compile command: Javac Helloworld.java

If the program has no errors, a Helloworld.class file with the same name will be generated in the current directory. At this point, enter Java HelloWorld on the command line to see the program execution result: Hello java

To verify its cross-platform features, we take the. class to the Windows platform to verify:

The result is the same.

Java command-line functionality is very rich, no longer one by one to repeat, of course, smart people will still use a good IDE.

Five, bytecode how to interpret?

What does the bytecode look like? Open the. class file to see:

This is the byte code of the above HelloWorld, which is indeed presented in bytes. Four not four looks dizzy, four not four except the beginning of Cafebabe other do not know, in fact, you just know that byte code is to use byte corresponding to the keyword, such as the beginning of the cafebabe corresponding to the file encoding format, corresponding to the number of magic, the following 0034 represents the version number.

Why would you do that? We go back to the second question: How the program is executed, the machine can understand the binary machine code, but we do not translate the compiler directly into the "0" and "1" ah, or so many programming languages, so many compilers, will be dead drop, Learn the basics of computer should be familiar with such a picture (image from Baidu Encyclopedia):

Yes, high-level language to machine language and a layer of assembly language, which is a symbolic language, easy to get started, difficult to write, usually used in the bottom, hardware drivers, embedded operating system and real-time running program all need assembly language, that is, high-level language compiler is not directly to the code into binary, The second is to turn into assembly language, and then through the assembly instructions into machine code. Back to Java, bytecode and assembly language in fact there is a correspondence between, of course, the bytecode to be more rich.

On the interpretation of bytecode, not one or two words can be said clearly, another day pits, if really interested, you can go to read a book, "in Layman's JVM", there are several interpretation of the portal as follows:

http://blog.csdn.net/dc_726/article/details/7944154/

Http://www.importnew.com/13107.html

Vi. What is the meaning of decoding bytecode?

Can let you understand the implementation principle of the program, easy to understand after the optimization of code, of course, also capable of bad things ...

Chat about Java bytecode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.