r/learnjava • • Nov 03 '20

Why is c faster than java?

If I were to make the same thing in java would it now be as fast ?

58 Upvotes

26 comments sorted by

View all comments

66

u/gtrley Nov 03 '20

Something along the lines of C is a lower level language and runs closer to the hardware, considering Java runs in the JVM

7

u/IIwarrierII Nov 03 '20

But once java is compiled isn’t it fast ?

5

u/__Ambition Nov 04 '20

Java compiles to bytecode. Bytecode is then run by a Virtual Machine which is roughly speaking, a software emulation of the CPU.

C on the other hand compiles to machine code, which runs on the actual CPU. Since Java's Virtual Machine software runs on top of the actual machine hardware, it's slower. C has no such software layer, it runs directly on hardware once compiled.

(Note that this an overly simplified explanation and things like JIT compilation make this a bit harder to ELY5.)