Seek Interview Questions: What is the difference between C++ and Java

Friday, August 27, 2010

What is the difference between C++ and Java

Ans). 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 Javasupports multiple interface inheritance, which allows an object to inherit many method signatures fromdifferent interfaces with the condition that the inheriting object must implement those inherited methods. Themultiple interface inheritance also allows an object to behave polymorphically on those methods. [Refer Q 8 and Q 10 in Java section.]
  • 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.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework (Java collection framework – Refer Q14, Q15 in Java section).
  • 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. [Refer Q32 in Java section].

No comments: