Why are compiled Java class files smaller than C compiled files?

后端 未结 9 1500
被撕碎了的回忆
被撕碎了的回忆 2020-12-16 17:16

I would like to know why the .o file that we get from compiling a .c file that prints \"Hello, World!\" is larger than a Java .class file that also prints \"Hello, World!\"?

9条回答
  •  情歌与酒
    2020-12-16 17:43

    A class file is Java byte code .

    It is most likely smaller since C/C++ libraries and operating system libraries are linked to the object code the C++ compiler produces to finally make an executable binary.

    Simply put, it is like comparing Java byte code to object code produced by a C compiler before it is linked to create a binary. The difference is the fact that a JVM interprets the Java byte code to properly do what the program is meant to do whereas C requires information from the operating system since the operating system functions as the interpreter.

    Also in C Every symbol (functions etc.) you reference from an external library at least once in one of the object files is imported. If you're using it in multiple object files, it's still imported just once. There are two ways this "importing" can happen. With static linking, the actual code for a function is copied into the executable. This increases file size but has the advantage that no external libraries (.dll/.so files) are needed. With dynamic linking this doesn't happen, but as a result your program requires additional libraries to run.

    In Java, everything is "linked" dynamically, so to speak.

提交回复
热议问题