Is the creation of Java class files deterministic?

前端 未结 11 1584
抹茶落季
抹茶落季 2020-12-02 11:04

When using the same JDK (i.e. the same javac executable), are the generated class files always identical? Can there be a difference depending on the

11条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 11:39

    Short Answer - NO


    Long Answer

    They bytecode need not be the same for different platform. It's the JRE (Java Runtime Environment) which know how exactly to execute the bytecode.

    If you go through the Java VM specification you'll come to know that this needs not to be true that the bytecode is same for different platforms.

    Going through the class file format, it shows the structure of a class file as

    ClassFile {
        u4 magic;
        u2 minor_version;
        u2 major_version;
        u2 constant_pool_count;
        cp_info constant_pool[constant_pool_count-1];
        u2 access_flags;
        u2 this_class;
        u2 super_class;
        u2 interfaces_count;
        u2 interfaces[interfaces_count];
        u2 fields_count;
        field_info fields[fields_count];
        u2 methods_count;
        method_info methods[methods_count];
        u2 attributes_count;
        attribute_info attributes[attributes_count];
    }
    

    Checking about the minor and major version

    minor_version, major_version

    The values of the minor_version and major_version items are the minor and major version numbers of this class file.Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m. Thus, class file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1. A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.1

    Reading more through the footnotes

    1 The Java virtual machine implementation of Sun's JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun's JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. Implementations of version 1.2 of the Java 2 platform can support class file formats of versions in the range 45.0 through 46.0 inclusive.

    So, investigating all this shows that the class files generated on different platforms need not be identical.

提交回复
热议问题