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++
- C++ (earliar known as C with Classes) was developed by Bjarne Stroustrup in 1979 at Bell Labs.
- C++ make use of Pointers and References
- C++ binary is not portable meaning binary cannot be run on different OS Platforms.
- C++ supports Multiple Inheretence through usage of Virtual Inheritance.
- Dynamic Polymorphism is achieved using Virtual Keyword (use of Virtual Pointer [VPTR] and VTable)
- 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)
- C++ uses Collections by using STL (Standard Template Library). Its a third-party API
- C++ can have Global functions or Global variables which are outside the class
- C++ uses namespaces
Java
- Java was developed by James Gosling in 1995 at Sun Microsystems (now acquired by Oracle Corporation).
- Java uses only References. Pointers are eliminated in Java to remove the complexity from the Programmers.
- Java code is portable and is achieved using Java Bytecode (.class) file which can be executed by JVM (JVMs differ based on the OS).
- Java doesn’t support Multiple Inheretence (because of Daimond Problem), But supportsInterface Inheritance using Interface.
- Dynamic Polymorphism is achieved using Method Overiding
- 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.
- Java API comes with Collections package.
- Java cannot have Global functions or Global variables
- Java uses Packages.