Web Development

General discussion


why is java slower than C

By satmeet ·
i need to understand a basic question that has been in mind for a long time .
it goes like this , jva is a language made with embedded system in mind , system that would exist in real word and would interact with each other and the user , so why is that java is slow , al system software is written in c /c++ .
why is that , what is that makes a language fast , i mean even when you use JIT compiler , what is it that needs to be done to make it fast , i want to find a solution to the above ,the question has many dimensions to it , one can say why can't we write on Operating system in Java too. maybe i am not aware of the above things , maybe i am wrong , but then again , please cearify the above. my email is satmeet@usa.net thanks .

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

It's a trade-off...

by privately_owed In reply to why is java slower than C

Because C is compiled specifically for a given hardware platform, while Java must do "runtime translation" into the native machine code for that platform. There's more to it than this, but the basic reason is that Java trades speed for flexibility, while C trades flexibility for speed.

Java has an extra layer between it and the CPU, the virtual machine, which takes care of translating between the universal Java byte code (what you get when you compile Java source) and the CPU-specific machine language code. The VM is why you can compile a Java program once and run it unmodified on several different platforms, but it comes with a lot of overhead.

With C on the other hand, you'd have to recompile your code each time, because the C compiler optimizes the code to the CPU it's designed to compile for, which make it usually much faster than Java, but not portable at all. Plus, C can directly access memory instead of going through a memory manager, which also tends to speed things up.Of course that leaves the door open for lots of nasty bugs too.

Related Discussions

Related Forums