c++ vs java之二

解释2:

Ques 03: What is the difference between C++ and Java?

Ans 03: Both C++ and Java use similar syntax and are Object Oriented, but:

Java does not support pointers. Pointers are inherently tricky to use and troublesome.

Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.

Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.

All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.

C++ requires explicit memory management, while Java includes automatic garbage collection.

解释3:

C++

  1. C++ (earliar known as C with Classes) was developed by Bjarne Stroustrup in 1979 at Bell Labs.
  2. C++ make use of Pointers and References
  3. C++ binary is not portable meaning binary cannot be run on different OS Platforms.
  4. C++ supports Multiple Inheretence through usage of Virtual Inheritance.
  5. Dynamic Polymorphism is achieved using Virtual Keyword (use of Virtual Pointer [VPTR] and VTable)
  6. C++ doesn’t support Memory Management. The progammer has to take care of Memory Allocation and Deallocation with the operations like New, Delete, Malloc, Calloc and use of Destructors (virtual destructors in case of virtual inheritance)
  7. C++ uses Collections by using STL (Standard Template Library). Its a third-party API
  8. C++ can have Global functions or Global variables which are outside the class
  9. C++ uses namespaces

Java

  1. Java was developed by James Gosling in 1995 at Sun Microsystems (now acquired by Oracle Corporation).
  2. Java uses only References. Pointers are eliminated in Java to remove the complexity from the Programmers.
  3. Java code is portable and is achieved using Java Bytecode (.class) file which can be executed by JVM (JVMs differ based on the OS).
  4. Java doesn’t support Multiple Inheretence (because of Daimond Problem), But supportsInterface Inheritance using Interface.
  5. Dynamic Polymorphism is achieved using Method Overiding
  6. Java support Automatic Memory Management using Garbage Collector. The GarbageCollector is a Daemon thread which keeps scanning the memory and clears the objects when the memory is low or required.
  7. Java API comes with Collections package.
  8. Java cannot have Global functions or Global variables
  9. Java uses Packages.

原文地址:https://www.cnblogs.com/liujiahi/p/2196344.html