Wednesday, October 9, 2019

Comparison between C++ and Java Research Paper Example | Topics and Well Written Essays - 1000 words

Comparison between C++ and Java - Research Paper Example Later it was adapted as the basis for â€Å"HotJava† thin client that depends on a virtual machine that is very portable and secure. Java is provided with a wide-ranging library that supports total abstraction for the fundamental platform. Java is a statically typed OOP like C++ and both languages display a similar syntax though their syntaxes are not compatible with each other. Moreover, Java was designed from scratch and was aimed at portability. (Deitel & Deitel, 2009) Comparison of Features Java and C++ differ significantly when it comes to certain features and implementation in each language. These are discussed below with examples to clarify the differences. Java does not possess multiple inheritances, instead it relies on interfaces. Interfaces in Java are comparable and largely similar to classes in C++ that have nothing except for virtual functions. Java allows inheritance from one base class only even if the other base classes are composed of little else than abstrac t methods (which are pure virtual functions). However Java allows the implementation of multiple interfaces which is similar to C++ and is nearly similar. (Horstmann & Cornell, 2008) An interface in Java for a Stack would be: public interface Stack { public void Push(Object o); public Object Pop(); }; The structure for C++ is similar and is shown below: class Stack { public: virtual void Push(Object&) = 0; virtual Object& Pop() = 0; }; It must be borne in mind that an interface in Java is not a class and any functions declared in a Java interface are not implementable in the same interface. A Java interface is not allowed any member variables either. This helps Java to eliminate the multiple virtual inheritance problems seen in C++ as it is not possible to inherit identical member variables from more paths than one. Virtual inheritance creates difficulties for the programmers of all kinds. Java solves this by eliminating multiple inheritances of classes by permitting multiple interf ace implementations. This has simplified Java appreciably in comparison to C++. However there is a drawback too. Java does not allow inheriting implementation from multiple classes in cases where there are no identical member variables too. Most coding situations require that variables have multiple inheritances from base classes that possess data and functions. While C++ can help implement these through careful variable naming, Java disallows it altogether. Another aspect of major difference is garbage collection. Garbage collection is a way to manage memory by freeing memory blocks that are not being referred to anymore. For example if the Java code presented below is considered: Clock a = new Clock(); // a is a new clock // ... a is used for a while a = null; // a is set to null and the system will remove it afterwards The code above creates a new Clock object by using the keyword new. The variable a is used to refer to this new object. a has a similar structure to reference vari ables used in C++ but Java allows the reassignment of references unlike C++. The variable a is used for some time after which it is reset to null. The Java runtime system keeps a in watch and when it detects that there are no more references to a, it brands a as garbage and clears it up. This aids in returning memory to the heap. In comparison C++ does not offer such an extensive garbage collection system. Although third party freeware and

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.