<Tr> <Td> vector (i) + +; </Td> <Td> mov edx, (ebp + 4h) mov eax, (ebp + 1Ch) inc dword ptr (edx + eax * 4) </Td> <Td> aload_1 iload_2 dup2 iaload iconst_1 iadd iastore </Td> </Tr> <P> Since performance optimizing is a very complex issue, it is very difficult to quantify the performance difference between C++ and Java in general terms, and most benchmarks are unreliable and biased . Given the very different natures of the languages, definitive qualitative differences are also difficult to draw . In a nutshell, there are inherent inefficiencies and hard limits on optimizing in Java, given that it heavily relies on flexible high - level abstractions, however, the use of a powerful JIT compiler (as in modern JVM implementations) can mitigate some issues . In any case, if the inefficiencies of Java are too great, compiled C or C++ code can be called from Java via the JNI . </P> <P> Some inefficiencies that are inherent to the Java language include, mainly: </P> <Ul> <Li> All objects are allocated on the heap . For functions using small objects, this can result in performance degradation and heap fragmentation, while stack allocation, in contrast, costs essentially zero . However, modern JIT compilers mitigate this problem to some extent with escape analysis or escape detection to allocate objects on the stack, since Oracle JDK 6 . </Li> <Li> Performance - critical projects like efficient database systems and messaging libraries have had to use internal unofficial APIs like sun. misc. Unsafe to gain access to manual resource management and be able to do stack allocation; effectively manipulating pseudo-pointers . </Li> <Li> Methods are virtual by default (although they can be made final), usually leading to an abuse of virtual methods, adding a level of indirection to every call . This also slightly increases memory use by adding one pointer per object to a virtual table . It also induces a start - up performance penalty, since a JIT compiler must perform added optimizing passes to devirtualize small functions . </Li> <Li> A lot of run - time casting required even using standard containers induces a performance penalty . However, most of these casts are statically eliminated by the JIT compiler . </Li> <Li> Safety guarantees come at a run - time cost . For example, the compiler is required to put appropriate range checks in the code . Guarding each array access with a range check is not efficient, so most JIT compilers will try to eliminate them statically or by moving them out of inner loops (although most native compilers for C++ will do the same when range - checks are optionally used). </Li> <Li> Lack of access to low - level details prevents the developer from improving the program where the compiler is unable to do so . </Li> <Li> The mandatory use of reference - semantics for all user - defined types in Java can introduce large amounts of superfluous memory indirections (or jumps) (unless elided by the JIT compiler) which can lead to frequent cache misses (a.k.a. cache thrashing). Furthermore, cache - optimization, usually via cache - aware or cache - oblivious data structures and algorithms, can often lead to orders of magnitude improvements in performance as well as avoiding time - complexity degeneracy that is characteristic of many cache - pessimizing algorithms, and is therefore one of the most important forms of optimization; reference - semantics, as mandated in Java, makes such optimizations impossible to realize in practice (by neither the programmer nor the JIT compiler). </Li> <Li> Garbage collection, as this form of automatic memory management introduces memory overhead . </Li> </Ul>

How does java differ from c and c++