Java Class Files
What is a .class file?
Java class files, which have a filename extension .class, contain Java bytecode, which can be executed on the Java Virtual Machine (JVM). These .class files are produced when the Java compiler compiles .java source files. The javac command line instruction produces .class files, and Eclipse automatically compiles .java files and places them in the /bin folder of projects.
You'll learn more about compilation in CS 61C and CS 164, but for now, it is sufficient to know that compilation translates programmer-produced source code into Java bytecode, which is an instruction set language interpretable by the Java virtual machine. Refer to the first lab regarding the various jobs the compiler does in translating code, checking errors, and optimizing instructions.
The great thing about Java is that it is platform-independent. Some languages are compiled to bytecode or object files that are specific to the operating system in which they are compiled. For these sorts of languages (e.g. C), files that result from compilation on a Unix machine may not run on a Windows machine. On the other hand, a Java .class file that is compiled in one platform will execute in the JVM (Java Virtual Machine) of any other platform.
Java developers can thus send each other .class files, which they can use to run programs without having the original .java source code. And because .class files are intended for the Java virtual machine, not for human developers, they are generally just run without being modified. (Note: It is possible to decompile .class files, but it is generally not commonly done.)
Hello World .java File
This simple HelloWorld.java file, when compiled, becomes this HelloWorld.class file that is clearly not very readable by human developers. Something interesting to see, however, is how the .class file clearly lists out the complete package names for things like PrintStream, which are used implictly when calling System.out.println.
Hello World .class File
Because developers cannot read .class files, they should also provide an API (Application Programming Interface) along with their code. This API documentation specifies all public variables and method headers of all public methods and what types of exceptions methods throw. This documentation is necessary else the code cannot be used by other developers.