11

I was following a tutorial video on compilers on YouTube, when I came across the fact that the C Programming Language is faster because it allows both static linking and dynamic linking, but Java allows only dynamic linking and that is why C is much faster than Java.

My question is, if static linking makes a program run faster, why was it not included in Java? I know there must be some real good reason why this decision was taken by the developers of Java to not include static linking, I just want to know what are the reasons.

Note : I do not know if this question already has an answer on SO, but since I could not find one, so I posted. If the answer does already exist, please provide a link to it.

Note : The link to the tutorial provided is in Hindi Language. Sorry about that.

15
  • 9
    that is why C is much faster than Java - really? Commented Dec 4, 2015 at 9:31
  • 2
    Apart from what Andreas said - why would the support of static linking make a programming language "faster" overall? Commented Dec 4, 2015 at 9:31
  • 2
    Related: stackoverflow.com/questions/1993390/… Commented Dec 4, 2015 at 9:35
  • 4
    Performance of C vs. Java is much discussed and highly disputed, so I would not classify that as a statement but as an opinion that has to be looked at in the context it was made. Commented Dec 4, 2015 at 9:36
  • 4
    "Thus C is much faster than Java". Is this statement wrong? - yes, as it stands it is definitely wrong. You can not make such a statement without additional information about the concrete scenario. What he probably meant is that the startup phase is faster - but again, first that might be correct for simple applications but different for complex ones, and second you need to decide whether it matters (as startup only occurs once when you launch the application). Then, this might be an issue for Desktop applications, but less an issue for server applications. Commented Dec 4, 2015 at 9:43

3 Answers 3

13

Java does not include a linker step at compile time. With Java 9 there will be a tool (jlink: JEP 275, JavaOne Talk on Project Jigsaw) which will create an image that will have the dependencies linked in.

One of the main goals of Java when it was created was "Code once, run everywhere". Statically linking environment dependent libraries or code parts in will negate this feature.

Sign up to request clarification or add additional context in comments.

5 Comments

To be pedantic: Java does include a linker step which is what the resolve parameter in class loading indicates ;) In particular, if you write naive code that crosses classloader boundaries you will encounter exceptions from the linker phase of class loading because classloaders also act as namespaces. (Meaning the name com.acme.A in classloader C1 is different from the name come.acme.A in classloader C2.)
So, the only reason to go for dynamic linking is for the "Code once, run everywhere" feature, or is there something else too? Just asking.
@SajibAcharya No, it's just plain wrong. Conceptually, nothing prevents anybody from "statically linking" Java programs. Or if something does, this answer does not explain why.
I don't understand how static linking negates "Code once, run everywhere". There are languages(Go, Rust etc.) using static linking which can be containerized and run even with distroless or scratch images. Java is dynamically linked, has the slogan of "Code once, run everywhere", but it still requires JRE to run.
@Hmerac I'm not a compilers expert so this is just my reasoning here - Java is compiled to .class files (so called byte code) and not to assembly (machine code). Those class files could then be distributed and interpret in any JVM in any OS, yielding the same result. On the opposite side the compilation from C code to machine code means translating the source code to a very specific instructions that are not reusable in different OSs or architectures.
6

C is not faster because of static versus dynamic linking. That is a red herring.

Java loading times are typically poor because the VM and standard library are comparatively huge, which means there simply is a lot to load.

Indeed, dynamic linking can be used to speed up initial loading times because with dlopen() and friends you control when that loading happens instead of paying the cost upfront (during loading).

Within this context, in general the one major difference in performance is in memory, in particular the ability to control layout of memory to some extent in C. That can yield substantial benefits because with less fragmentation and by reducing things down to 'cache size' CPU caching and speculative hardware optimisations (like prefetching) work much better.

Comments

5

Static linking in Java would force the compiler to add the whole JRE libraries in each application, which would be a waste of memory and it would break the compile once/run anywhere advantage of Java.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.