Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the class file generated by javac always the same?

Tags:

javac

Currently we are in the process of re-scripting all of our build system for a large project (around 2000 source files) and there has been talk of doing a binary comparison on the files to ensure that everything is correct which leads to the following question: Is the output of javac guaranteed to be same across compilations or could it be subject to change?

Another question implied that the constant pool could have a different order, but assuming we are able to control for the order of the files going into the javac call is there still a potential for differences? We are using Ant and Maven as part of the build if that may influence things as well.

like image 287
rjzii Avatar asked Mar 29 '12 15:03

rjzii


People also ask

How does the .class file get generated?

class" file is created as a result of successful compilation by the Java compiler from the ". java" file. Each class in the . java file is compiled into a separate class file if the ".

How many class files will be generated when we compile?

For Compiling: After compilation there will be 3 class files in corresponding folder named as: Sample. class.

Are .class files generated?

Answer-> A class file generated when the compiler starts the compilation and exactly at that time a interpreter(JRE) generates class file.

Does a class have same name as file?

The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point. Suppose when we create a program in which more than one class resides and after compiling a java source file, it will generate the same number of the .


1 Answers

The bytecode is absolutely not guaranteed to be the same; for one thing, compilers are allowed to perform optimizations that don't affect any guaranteed behaviors. The Java Language Specification even mentions, in a few places, optimizations that a compiler might perform; for example, of the string concatenation operator +, it notes that:

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

[link]

like image 115
ruakh Avatar answered Oct 23 '22 07:10

ruakh