Forget the obvious fact that one of the two categories of transportation is highly specialized and that both operate in different spaces and are bound by different rules and suitabilites for tasks.
|
GC and manual memory management are bound by quite different rules, and are suitable for different tasks, so the analogy holds. Manual memory management is great if you have very little memory, GC is great in all the other cases. However, you are right, that the example with a car is wrong, because there is probably no malloc implementation even remotely close in performance to increasing a single pointer. The best malloc implementations require about 60 CPU cycles on average per malloc of a small object, for a single-threaded program (and default malloc in Linux / Windows require 100+ cycles, and this gets worse in case of multithreading). Contrary, Oracle's JVM requires 10 CPU cycles per allocation on average, so?
The guys from IBM describe in detail, why allocation in Java is much faster (and probably .NET, because most things for Java are true also for .NET):
http://www.ibm.com/developerworks/java/library/j-jtp09275/index.html
Add to that, that JVMs can now even eliminate some calls to new and replace them with stack allocation, making a cost of allocation essentially 0. I wish C++ compiler could do that, one common probem of library designers - "should this be passed through stack, or maybe I should return a pointer to a newed object?" would be solved.
When you tack on final to your class declaration the jvm does some optimization it can't do otherwise. |
Nice explanation, but completely wrong. Final has nothing to do with performance, maybe it had 10 years ago. Modern JVMs don't require that information to apply optimisations - forgetting to add final does not penalise optimisations in any way.
Two of the most important applications of final is implementing immutable classes and strenghtening security related stuff (e.g. so that you cannot override finalize method to bypass security checks in the constructor of the attacked class).
One thing I find that's pretty irksome about Java is the abundance of exception handling. |
Well, this is because Java has usable exception handling (could be better, but at least it is usable). Because it is ok, people just use it. C++ exception handling is troublesome, because it does not play well with manual memory management, it is hard to get exception-safety right, so programmers stay away from it as far as they can.