Вы находитесь на странице: 1из 6

Critical Comparison between Java and C++

It has been evident that there have always been arguments among the developers society on which programming language is better time to time. But In a nutshell, each language technology has its own pros and cons. Therefore, it is not at all easy to conclude that one language is better than the others. This technical write-up is not intended to outclass one language over the other but to compare and contrast the two language technology and summing up why I have preferred a particular language technology over the other with regard to project type, scope, constraints and language features and support to develop a particular system. The language technology I adopted to develop this project is Java, which is relatively new to the language that I decided to compare with - that is C++. Java and C++, an enhancement of C language are two of the most widely embraced technologies in the software development industry presently. First of all Java is purely object oriented and can be said it is an epitome of object orientation since Java is all about object (any real world thing that is used to develop system), class (categorization of similar objects and used as a blueprint for object creation), data (attributes) and methods (behaviors) which define the characteristics of object. However, C++ is not a purely object oriented but rather a language that has object oriented features. That is, C++ is basically a C language with the extension of object oriented capabilities. Therefore, we can say C++ is a hybrid between structured (procedural) and object oriented. It is implicit that all classes in Java are subclasses of Object (root of all classes). This is significantly different from C++ where it is possible to create inheritance trees that are completely unrelated to one. Further, in Java all functions (methods) are part of a certain class i.e., specific that class. So an object instantiated from that class can make use of those functions. To be concise, there are no independent functions. But in C++, there are standalone functions that are independent to each other and classes Java doesnt provide the feature of multiple inheritance (that is, one subclass inheriting data and methods from more than one super class) while its former C+ + does support multiple inheritance. The designers of Java purposely avoided multiple inheritance as they thought multiple inheritance would lead to increase in complexity of the program. Although purposely removed, they provided with the mechanism called interfaces through which the concept of multiple inheritance can be achieved to a certain extent. . C++ does not have formal interface specifications as Java. A Java interface is almost identical to C++ class that has nothing but pure virtual functions. Virtual functions are functions defined in a class that can be overridden (redefining a same function) in the subclasses, which is one of the ways of achieving polymorphism (functions that are meant for same objective but each

function performs the same operation in a different manner.). In java, by default all the non-static methods are virtual functions unless it is marked with final keyword. In C++, it has to be declared with virtual keyword. In java, you cannot inherit from more than one base class; even if the base class has nothing but merely abstract methods (pure virtual functions). However, you can implement more than one interface to a same class. But interface is not equivalent to class in Java. Java interface cannot have data members and also functions declared in Java interface cannot be implemented within that interface. Therefore, Java interface does not lead to problems that are inherent in C++ virtual inheritance as virtual inheritance is a complex feature and creates problems for compiler, implementers and application programmers alike. Deadly Diamond of Death (DDD) is a name called for the problem caused by multiple inheritance in C++. This diagram shows four classes arranged in the diamond structure that creates the need for virtual inheritance. Both of the classes B and C inherit from class A. D multiply inherits from both B and C. Class B and C will have the different implementation of method f(). Class D inherits from class B & C. According to the principle of inheritance, it should inherit all the methods of the parent classes B & C. But we have a common method in those classes but with different implementations. Now the question is which implementation will be used for the last child class which inherits both these classes? To avoid this sort of critical issue, Java banned multiple inheritance. The class diagram which is formed above is like that of a diamond, but with no solution or outcome, and so it is called Deadly Diamond of Death. C++ supports operator overloading while Java doesnt support operator overloading. Operator overloading allows the compiler to instruct how to perform certain operation when its corresponding operator is used and you can give special meanings to operators, when they are used with user-defined classes. This is sometimes called adhoc polymorphism. Although this feature is also available in certain other languages Java designers found that code that makes heavy use of operator overloading is harder to read and harder to maintain because it is severely restricted syntactically and semantically (since you will be

using symbols that have very precise mathematical definitions, it is very unlikely that your usage will match the mental model that people who read your code have of their meaning). In Java, all methods (except non static and final methods) are dynamically bound at the run time. In C++, virtual functions are dynamically bound. Dynamic binding refers to the scenario that calling of method is handled at runtime not compile time. Dynamic binding in Java binds the method calls based on the actual object type and not on the declared type of the object reference. Reference: oop.html http://www1.cse.wustl.edu/~levine/courses/cs342/c++/javaVcpp-

Up to now, we looked into the issues that exist between Java and C++ with regard to object orientation. Now we look upon the general programming issues. Class definition in both languages is almost same but in C++, class definition ends with a semicolon after closing brace. In Java, methods are defined inside the class definition whereas in C++, methods are outside the class definition. For C++ programmer, Java methods may look like inline functions but they are not. The main method where the execution of a program begins cannot return a value in Java but in C++ it can. In Java, everything must be contained within a class. And Unlike C++, there are no global functions or global data in Java. Anyway, static methods and static data within a class are somewhat equivalent to global data and functions. But both C+ + and Java support class (static) methods or functions that can be called without the requirement to instantiate an object of the class C++ makes use of preprocessor (hash # symbol) heavily, while Java doesnt support preprocessor instead they use import keyword - for example, if you want use classes from another package/library. C++ has comma (,) operator, which is used to separate two or more expressions. For example, a = (b=3, b+2); the value 3 is assigned to b, and then value of b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3. In Java, there is no such comma operator. And theres no scope resolution operator (::) in Java. Java uses the dot for everything since everything is defined only within a class. Since Therefore, there is no need for scope resolution there either. C++ supports goto statement but Java doesnt, although goto; is a reserved word in Java. However, it does support labeled break and continue statements, which are not available in C++. This labeled break and continue statements can be used in certain situations, where a goto statement might otherwise be used. Java has basically two types of data type namely, primitive and object type. C++ has various types such as char, byte, short, int, long, float, double, and structs, unions, enums, and arrays. Unlike C++, the size of each primitive type is the same regardless of the platform. There is no unsigned integer type in Java; all

numeric types are signed while C++ contains signed and unsigned numeric types. Type checking and type requirements are much tighter in Java than in C+ +. Java does not support automatic type conversions (except where guaranteed safe). The char type in C++ is an 8-bit type that maps to the ASCII (or extended ASCII) character set. The char type in Java uses the 16-bit type Unicode character set, so it can represent most national characters. There is no explicit Boolean data type in C++ while Java has explicit primitive boolean data type. Conditional expressions in Java must evaluate to Boolean. In the case of C++, it is evaluated to integer. Unlike C++, Java has a String type, and objects of this type cannot be modified (immutable). Quoted strings are automatically converted into String objects. Java also has a StringBuffer type. Objects of this type can be modified, and a variety of string manipulation methods are provided. Although Java uses the same keywords as C++ for access control: private, public, and protected, the interpretation of these keywords may vary between Java and C++. For example, protected in C++ means accessible to inheritors only but in Java, it means accessible to inheritors and to others in this package. In C++, if you do not specifically initialize variables of primitive types, they will contain garbage. Although local variables of primitive types can be initialized in the declaration, primitive data members of a class cannot be initialized in the class definition in C++. In Java, you can initialize primitive data members in the class definition. You can also initialize them in the constructor. If you dont initialize them, they will be initialized to zero (or equivalent) automatically. C++ allows the instantiation of variables or objects of all types either at compile time in static memory or at run time using dynamic memory. In Java, all variables of primitive types have to be instantiated at compile time, and all objects have to be instantiated in dynamic memory at runtime. In addition, wrapper classes are provided for all primitive types except byte and short to allow them to be instantiated as objects in dynamic memory at runtime if needed. Unlike C++, there are no explicit pointers in Java. Java does not allow programmer to modify the address contained in a pointer or to perform pointer arithmetic. Pointers in C++ allow the programmer to point at any place in memory, which is considered unsafe and that is why Java eliminates the need of explicit pointers. Pointers are often seen as an efficient way to move through an array of primitive variables; Java arrays are handled in a safer fashion by providing object types for arrays and strings. For example, C++ declaration char* ptr needed to point to the first character in a C++ null-terminated "string" is not required in Java as String is a true object in Java. In Java, size of an array can be retrieved using length member of array object. Using this length variable in a good manner, an array index bound exception can be prevented. But in C++, function sizeof returns the array size in bytes, not its

length. Array lengths in multidimensional arrays can vary from one element to the next within one dimension. Array lengths in multidimensional arrays are all the same size in a give dimension, fixed by the declaration. Java has constructors that are similar to constructors in C++. You get a default constructor if you dont define one, and if you define a non-default constructor, theres no automatic default constructor defined for you, just like in C++. Unlike C++, there are no destructors in Java but automatic garbage collection, which facilitates automatic memory management. Unused memory is returned to the operating system by way of a garbage collector, which runs in a different thread from the main program. In C++, programmers have to manually de-allocate memory space for objects. This is not only extra work for programmers but also results in memory leak leading to the gradual depletion of memory resources. But Java Virtual Machine (JVM) automatically reclaims memory makes it easier for programmers by removing the need to explicitly de-allocate objects. It helps ensure program integrity as there is no way of accidentally crashing the JVM by incorrectly freeing memory. Despite the advantages of automatic garbage collection, there is potential disadvantage that it may cause overhead that can affect program performance. The JVM has to keep track of which objects are being referenced by the executing program, and finalize and free unreferenced objects. This activity will likely require more CPU time than if the program explicitly frees unnecessary memory. In addition, programmers in a garbagecollected environment have less control over the scheduling of CPU time devoted to freeing objects that are no longer needed. However, since there are very good garbage collection algorithms been developed and Java's garbage collector runs in its own thread, it will run transparently alongside the execution of the program. Plus, if a programmer really wants to explicitly request a garbage collection at some point, System.gc() or Runtime.gc() can be invoked, which will fire off a garbage collection at that time. C++ programs are compiled; Java programs are compiled as well as interpreted. This makes Java slower than its former in execution of programs. However, Java is platform independent Java crowd always utters the slogan write once, run everywhere, while C++is architecture specific. Java provides more support for web and distributed systems than C++, as one of Javas core concepts is method message passing between objects that can be easily modeled to distributed environment. And Java has better exception handling mechanism than C++ as exception class hierarchy facilitates programmers to handle almost all types of exceptions that may occur. Unlike C++, Java has built-in support for program documentation. Specially written comments can be automatically stripped out using a separate program named javadoc to produce program documentation. In conclusion, by comparing and contrasting the feature sets both the languages and carefully pondering the technical information (obtained from Java API and C+ + documentation) I opted for Java over C++ as Java is purely object oriented and less complex (less confusing features). In a nutshell, Java is robust due to

automatic memory management (thereby memory leaks are prevented), meaningful syntax and semantics, support for web services and distributed environment, multithreading (increase in performance), etc which will increase the programmers productivity and help engineering a system of high quality. C++ and Java Syntax Differences http://www.cprogramming.com/tutorial/java/syntax-differences-java-c++.html

Вам также может понравиться