The differences between the programming languages C++ and Java can be traced to their heritage, as they have different design goals.
Java is a general-purpose, concurrent, class-based, object-oriented programming language that is designed to minimize implementation dependencies. It relies on a Java virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide abstraction of the underlying platform. Java is a statically typed object-oriented language that uses a syntax similar to (but incompatible with) C++. It includes a documentation system called Javadoc.
The different goals in the development of C++ and Java resulted in different principles and design trade-offs between the languages. The differences are as follows:
The solution to this is to null out object references. A second common reason for memory leak is the use of cache that has become no longer relevant. The solution to memory leaks due to using old cache is to represent the cache using a WeakHashMap.
In addition to running a compiled Java program, computers running Java applications generally must also run the Java virtual machine (JVM), while compiled C++ programs can be run without external applications. Early versions of Java were significantly outperformed by statically compiled languages such as C++. This is because the program statements of these two closely related languages may compile to a few machine instructions with C++, while compiling into several byte codes involving several machine instructions each when interpreted by a JVM. For example:
Since performance optimization 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.
However, there are a number of benefits to Java's design, some realized, some only theorized:
The C++ language evolves via an open steering committee called the C++ Standards Committee. The committee is composed of the creator of C++ Bjarne Stroustrup, the convener Herb Sutter, and other prominent figures, including many representatives of industries and user-groups (i.e., the stake-holders). Being an open committee, anyone is free to join, participate, and contribute proposals for upcoming releases of the standard and technical specifications. The committee now aims to release a new standard every few years, although in the past strict review processes and discussions have meant longer delays between publication of new standards (1998, 2003, and 2011).
The lack of a firm standard for Java and the somewhat more volatile nature of its specifications have been a constant source of criticism by stake-holders wanting more stability and conservatism in the addition of new language and library features. In contrast, the C++ committee also receives constant criticism, for the opposite reason, i.e., being too strict and conservative, and taking too long to release new versions.
"C++" is not a trademark of any company or organization and is not owned by any individual.
"Java" is a trademark of Oracle Corporation.
Bloch 2018, pp. xi–xii, Foreword. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, p. 285, Chapter §11 Item 66: Use native methods judiciously. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, p. 285, Chapter §11 Item 66: Use native methods judiciously. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
"Unsigned Integer Arithmetic API now in JDK 8". Archived from the original on 25 February 2017. Retrieved 17 March 2014. https://web.archive.org/web/20170225213350/https://blogs.oracle.com/darcy/entry/unsigned_api
"The Java Tutorials: Passing Information to a Method or a Constructor". Oracle. Retrieved 17 February 2013. http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
"The Java Tutorials: Object as a Superclass". Oracle. Retrieved 17 February 2013.. http://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
"XMPP Software » Libraries". xmpp.org. Retrieved 13 June 2013. http://xmpp.org/xmpp-software/libraries/
Bloch 2018, pp. xi–xii, Foreword. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Goetz et al. 2006, p. 48, §3.4.1 Final fields. - Goetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). Java Concurrency in Practice. Addison Wesley. ISBN 0-321-34960-1. https://archive.org/details/javaconcurrencyi00goet
Goetz et al. 2006, p. 48, §3.4.1 Final fields. - Goetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). Java Concurrency in Practice. Addison Wesley. ISBN 0-321-34960-1. https://archive.org/details/javaconcurrencyi00goet
Robert C. Martin (January 1997). "Java vs. C++: A Critical Comparison" (PDF). Archived from the original (PDF) on 11 May 2008. Retrieved 15 December 2007. https://web.archive.org/web/20080511205821/http://www.objectmentor.com/resources/articles/javacpp.pdf
"Reference Types and Values". The Java Language Specification, Third Edition. Retrieved 9 December 2010. http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3
Horstmann, Cay; Cornell, Gary (2008). Core Java. Vol. I (Eighth ed.). Sun Microsystems. pp. 140–141. ISBN 978-0-13-235476-9. Some programmers (and unfortunately even some book authors) claim that the Java programming language uses call by reference for objects. However, that is false. Because this is such a common misunderstanding, it is worth examining a counterexample in some detail... This discussion demonstrates that the Java programming language does not use call by reference for objects. Instead object references are passed by value. 978-0-13-235476-9
Deitel, Paul; Deitel, Harvey (2009). Java for Programmers. Prentice Hall. p. 223. ISBN 978-0-13-700129-3. Unlike some other languages, Java does not allow programmers to choose pass-by-value or pass-by-reference—all arguments are passed by value. A method call can pass two types of values to a method—copies of primitive values (e.g., values of type int and double) and copies of references to objects (including references to arrays). Objects themselves cannot be passed to methods. 978-0-13-700129-3
"Semantics of Floating Point Math in GCC". GNU Foundation. Retrieved 20 April 2013. https://gcc.gnu.org/wiki/FloatingPointMath
"Microsoft c++ compiler, /fp (Specify Floating-Point Behavior)". Microsoft Corporation. Retrieved 19 March 2013. http://msdn.microsoft.com/en-us/library/e7s85ffb.aspx
"Java Language Specification 4.3.1: Objects". Sun Microsystems. Retrieved 9 December 2010. http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. xi–xii, Foreword. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 87–92, Chapter §8 Item 8: Favor composition over inheritance. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Goetz et al. 2006, pp. 25–26, §2.3.1 Intrinsic locks. - Goetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). Java Concurrency in Practice. Addison Wesley. ISBN 0-321-34960-1. https://archive.org/details/javaconcurrencyi00goet
Bloch 2018, pp. 126–129, Chapter §11 Item 78: Synchronize access to shared mutable data. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Standard for Programming Language C++ '11, 5.3.2 Increment and decrement [expr.pre.incr].
The Java™ Language Specification, Java SE 7 Edition, Chapters 15.14.2, 15.14.3, 15.15.1, 15.15.2, http://docs.oracle.com/javase/specs/ http://docs.oracle.com/javase/specs/
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 29–33, Chapter §2 Item 8: Avoid finalizers and cleaners. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 34–36, Chapter §2 Item 9: Prefer try-with-resources to try-finally. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 34–36, Chapter §2 Item 9: Prefer try-with-resources to try-finally. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 34–36, Chapter §2 Item 9: Prefer try-with-resources to try-finally. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Satish Chandra Gupta; Rajeev Palanki (16 August 2005). "Java memory leaks – Catch me if you can". IBM DeveloperWorks. Archived from the original on 22 July 2012. Retrieved 2 April 2015. https://web.archive.org/web/20120722095536/http://www.ibm.com/developerworks/rational/library/05/0816_GuptaPalanki/
How to Fix Memory Leaks in Java by Veljko Krunic (10 Mar 2009) https://web.archive.org/web/20140205030750/http://www.openlogic.com/wazi/bid/188158
Creating a memory leak with Java on stackoverflow.com https://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Bloch 2018, pp. 123–125, Chapter §2 Item 7: Eliminate obsolete references. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
"Type alias, alias template". cppreference.com. Retrieved 4 October 2022. http://en.cppreference.com/w/cpp/language/type_alias
"Variable template". cppreference.com. Retrieved 4 October 2022. http://en.cppreference.com/w/cpp/language/variable_template
Boost type traits library http://www.boost.org/libs/type_traits/doc/html/boost_typetraits/reference.html
Java Generics Are Turing Complete https://arxiv.org/abs/1605.05274
C++ Standards Committee. (2018). P0581R1 - Modules for C++. Retrieved from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0581r1.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0581r1.pdf
C++ Standards Committee. (2021). P2412R0 - Further refinements to the C++ Modules Design. Retrieved from https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2412r0.pdf https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2412r0.pdf
Clark, Nathan; Amir Hormati; Sami Yehia; Scott Mahlke (2007). "Liquid SIMD: Abstracting SIMD hardware using lightweight dynamic mapping". Hpca'07: 216–227.
Hundt, Robert (27 April 2011). "Loop Recognition in C++/Java/Go/Scala" (PDF). Stanford, California: Scala Days 2011. Archived (PDF) from the original on 9 October 2022. Retrieved 17 November 2012. Java shows a large GC component, but a good code performance. [...] We find that in regards to performance, C++ wins out by a large margin. [...] The Java version was probably the simplest to implement, but the hardest to analyze for performance. Specifically the effects around garbage collection were complicated and very hard to tune; 318 kB https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf
Matthew Hertz, Emery D. Berger (2005). "Quantifying the Performance of Garbage Collection vs. Explicit Memory Management" (PDF). OOPSLA 2005. Archived from the original (PDF) on 6 July 2017. Retrieved 15 March 2015. In particular, when garbage collection has five times as much memory as required, its runtime performance matches or slightly exceeds that of explicit memory management. However, garbage collection's performance degrades substantially when it must use smaller heaps. With three times as much memory, it runs 17% slower on average, and with twice as much memory, it runs 70% slower. https://web.archive.org/web/20170706100244/https://people.cs.umass.edu/~emery/pubs/gcvsmalloc.pdf
Alexandrescu, Andrei (2001). Addison-Wesley (ed.). Modern C++ Design: Generic Programming and Design Patterns Applied. Chapter 4. pp. 77–96. ISBN 978-0-201-70431-0. 978-0-201-70431-0
"Boost Pool library". Boost. Retrieved 19 April 2013. http://www.boost.org/doc/libs/release/libs/pool/
Targeting IA-32 Architecture Processors for Run-time Performance Checking https://web.archive.org/web/20200929080449/https://www.slac.stanford.edu/comp/unix/.../icc/.../optaps_dsp_qax.htm
"Fixing The Inlining "Problem" by Dr. Cliff Click |Azul Systems: Blogs". Archived from the original on 7 September 2011. Retrieved 23 September 2011. https://web.archive.org/web/20110907092432/http://www.azulsystems.com/blog/cliff/2011-04-04-fixing-the-inlining-problem
Bloch 2018, pp. 126–129, Chapter §11 Item 78: Synchronize access to shared mutable data. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
Oracle Technology Network for Java Developers http://java.sun.com/performance/reference/whitepapers/6_performance.html#2.1.2
"Working Draft, Standard for Programming Language C++" (PDF). Archived (PDF) from the original on 9 October 2022. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf
The Java Language Specification http://java.sun.com/docs/books/jls/
The Java Community Process(SM) Program - Participation - JCP Members http://www.jcp.org/en/participation/members
Bjarne Stroustrup's FAQ: Do you own C++? http://www.stroustrup.com/bs_faq.html#revenues
ZDNet: Oracle buys Sun; Now owns Java Archived 10 April 2010 at the Wayback Machine. http://blogs.zdnet.com/BTL/?p=16598