简体   繁体   中英

How does JVM understand the byte code compiled by both the JDK and Eclipse while both are different?

Compiled byte code by Eclipse and JDK are different. Then how does the JVM understand both the byte codes? Example:

package com.hcl.raj.utilities;

public class StringTest {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String name = "Rajkumar";
    byte[] byteCodes = new byte[] {15,22,35,48};
    String str1 = new String(byteCodes);
    System.out.println(str1);
  }
}

The compiled code of the above source code by JDK and Eclipse are different.

Original attempt to post compiled code removed due to encoding and formatting issues

First of all, is the bytecode different, or is the difference only in other parts of the class file? A class file can contain a lot of optional information (for debugging).

But even if the bytecode itself is different, they behave like synonyms. Even in a language as tightly defined as Java bytecodes, you can express the same behaviour in different ways.

For example, switch/case statements can be compiled into either of two different opcodes: tableswitch or lookupswitch . They both do the same thing essentially, so in certain cases a compiler is more or less free to choose between them.

What is important though that these differences are all within the limits of standard bytecode, regardless of platforms or any other external circumstances, no Java compliant compiler will produce bytecode that a similarly compliant VM can't interpret (assuming that they support the same version of Java).

There is a huge chapter in the JLS dedicated to specifying the Java class file format, which is really the whole point of having that format in the first place. The javac compiler holds no special authority on the format of the files it happens to produce.

Eclipse uses an SDK internally (which can be the same you used to directly generate your bytecode, or another one). Also, different SDKs can generate different Bytecode (for example if the JDK is from a diferent company or a diferent version), but if they meet the bytecode specification when they generate bytecode, they will be well interpreted by a JVM that also follows that specification.

Java bytecode is portable, this is one of key feature of java. You can read some info here . If you want to discover how this portability exactly works you can browse specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM