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

1

u/GoodLifeWorkHard Nov 04 '20

It mostly has to do with memory management. Java has automatic garbage collection for dynamically allocated memory whereas C does not. So when you compile & run a Java program, it automatically scans the file to see which objects should be garbage collected. This increases the duration / length of time for Java. In C, you have to specify which dynamically allocated variables are deallocated otherwise you get a memory leak.