Is the creation of Java class files deterministic?

前端 未结 11 1647
抹茶落季
抹茶落季 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:33

    There is no obligation for the compilers to produce the same bytecode on each platform. You should consult the different vendors' javac utility to have a specific answer.


    I will show a practical example for this with file ordering.

    Let's say that we have 2 jar files: my1.jar and My2.jar. They're put in the lib directory, side-by-side. The compiler reads them in alphabetical order (since this is lib), but the order is my1.jar, My2.jar when the file system is case insensitive , and My2.jar, my1.jar if it is case sensitive.

    The my1.jar has a class A.class with a method

    public class A {
         public static void a(String s) {}
    }
    

    The My2.jar has the same A.class, but with different method signature (accepts Object):

    public class A {
         public static void a(Object o) {}
    }
    

    It is clear that if you have a call

    String s = "x"; 
    A.a(s); 
    

    it will compile a method call with different signature in different cases. So, depending on your filesystem case sensitiveness, you will get different class as a result.

提交回复
热议问题