Posted By:
5 Feb, 2014 10:26 am PST

Advantages/Disadvantages of Java over C++

 How Java Differs From C++

 Java is a true object oriented language while C++ is basically with object-oriented extension. That is what exactly the increment operator ++ indicates. C++ has maintained backward compatibility with C. It is therefore possible to write an old style C program and run it successfully under C++. Java appears to be similar to C++ when we consider only the “extension” part of C++. However, some object-oriented features of C++ make the C++ code extremely difficult to follow and maintain.

Listed below are some major C++ features that were intentionally omitted from Java or significantly modified.

  Ø  Java does not support operator overloading.

  Ø  Java does not have template classes as in C++.

  Ø  Java does not support multiple inheritance of classes. This is accomplished using a new feature called “interface”.

  Ø  Java does not support global variables. Every variable and method is declared within a class and forms part of that class.

  Ø  Java does not use pointers.

  Ø  Java has replaced the destructor function with a finalize () function.

  Ø  There are no header files in Java.

Below are the differences specified in tabular form :

C++

Java

C++ is not a purely object-oriented programming, since it is possible to write C++ programs without using a class or an object.

Java is purely an object-oriented programming language, since it is not possible to write a Java program without using at least one class.

Pointers are available in C++.

We cannot create and use pointers in Java.

Allocating memory and de-allocating memory is the responsibility of the programmer.

Allocation and de-allocation of memory will be taken care of by JVM.

C++ has goto statement.

Java does not have goto statement.

Automatic casting is available in C++.

In some cases, implicit casting is available. But it is advisable that the programmer should use casting wherever possible.

Multiple inheritance feature is available in C++.

No multiple inheritance in Java, but there are means to achieve it.

Operator overloading is available in C++.

It is not available in Java.

#define, typedef and header files are available in C++.

#define, typedef and header are not available in Java, but there are means to achieve them.

There are 3 access specifiers in C++: private, public and protected.

Java supports 4 access specifiers: private, public, protected, and default.

There are constructors and destructors in C++.

Only constructors are there in Java. No destructors are available in this language.

Java also adds some new features. While C++ is a superset of C, Java is neither a superset nor a subset of C or C++. Java may be considered as a first cousin of C++ and a second cousin of C as illustrated in the below figure.

 

Advantages of Java over C++

 ·        One obvious advantage is a runtime environment that provides platform independence: you can use the same code on Windows, Solaris, Linux, Macintosh, and so on. This is certainly necessary when programs are downloaded over the internet to run on a variety of platforms.

· Garbage Collection: Java is known for its garbage collection. Garbage Collection (GC) is a form of automatic memory management. The garbage collector attempts to reclaim garbage, or memory, occupied by objects that are no longer in use by the program. This eliminates the problem of manual memory allocation. Thus, Garbage collection leads to lower bug count and faster execution time. Whereas in C++, a significant portion of C++ code is dedicated to memory management. Cross-component memory management does not exist in C++. Libraries and components are harder to build and have less natural APIs.

· The build process: When compared with Java, C++ builds are slow and complicated. Developers have noted that a full build in C++ might take up to 20 hours, while the same build might take only 7 minutes in Java.For debugging C++, developers need a second build. Java has more approachable build tools than C++.

· Safety:Java eliminates pointers, which can allow arbitrary memory access and the ability to easily crash the process (core dump). There are no buffer overruns in Java, and code and data cannot be accidentally mixed. And Java includes bounds-checking. Bounds-checking is any method of detecting whether a variable is within some bounds before its use.

· Performance: Although performance is typically not considered one of the benefits Java has over C++, garbage collection can make memory management much more efficient, thereby impacting performance. In addition, Java is multi-threaded while C++ does not support multi-threading. C++'s thread safe smart pointers are three times slower than Java references. And Java has HotSpot Java Virtual Machine (JVM), which features just-in-time (JIT) compilation for better performance.

· Reflection: Reflection is the ability of a program to examine and modify the structure and behaviour (specifically the values, metadata, properties and functions) of an object at runtime. Java has full runtime capability to look at the runtime. C++ has optional Run-time type information (RTTI) but no reflection. Reflection enables extremely powerful generic frameworks and provides the ability to learn about, access and manipulate any object.

· Standard System Type: Java has: specified, portable primitive types; a built-in, specified, portable runtime library; rich support for I/O, networking, XML/HTML, database connectivity; C++ does not.

· Portability: As Java's mantra has been "Write once, run anywhere," Java is portable with very little effort. C++ is portable in theory, but in practice, you have to build another language (#ifdef'd types, etc.) on top of it. And C++ has significant differences from vendor to vendor, e.g., standards support.

· Dynamic Linking: There is no standard way to dynamically link to C++ classes. Java allows arbitrary collections of classes to be packaged together and dynamically loaded and linked as needed. With Java, there are no dynamic link library fiascos, also known as DLL hell.

· Simplicity of Source Code and Artifacts: C++ splits source into header and implementation files. It requires a big monitor to see .hpp and .cpp header file extensions and file name extensions at the same time. With C++, there is code in multiple places: some inlined in the header, some in the .cpp. Artifacts are compiler-specific, but there are many of them. Yet, with Java, there is just one .java, one .class extension.

·       Due to its unconstrained expressiveness, low level C++ language features (e.g. unchecked array access, raw pointers) cannot be reliably checked at compile-time or without overhead at run-time. Related programming errors can lead to low-level buffer overflows and segmentation faults.In Java, low level errors either cannot occur or are detected by the JVM and reported to the application in the form of an excepion. It is far easier to turn out a bug- free code using Java than using C++.

 · Also Java eliminated the possibility of confusing an assignment with a test for equality in a conditional statement.

 Disadvantages of Java over C++

 · Full-Garbage collection (GC) pauses: Sooner or later, there is a part of GC that can’t be run in the background and can’t be avoided, which results in a temporary halt. This can be havoc for distributed systems. However, this can be partially improved by increasing memory footprint.

 · No Deterministic Destruction: C++ has deterministic destruction; Java does not. Deterministic destruction is useful for managing resources. In C++, when objects are deleted, their destructors are executed immediately. This allows system resources to be released when an object is no longer needed.

 · Barriers to native Integration: Operating systems are built in C/C++. APIs are typically C. Native GUIs and other OS-level functionality often requires C call backs. However, the Java Native Interface only allows Java to call native code that was written explicitly to be called by JNI.

 · Memory Footprint: Java uses significantly more memory than C++, particularly for "small" applications. Memory requirements limit Java adoption on some devices.

 

Attachment: image001.png


Video is posted for better understanding of the concept.

Was it useful?  



How Java Differs From C++

 Java is a true object oriented language while C++ is basically with object-oriented extension. That is what exactly the increment operator ++ indicates. C++ has maintained backward compatibility with C. It is therefore possible to write an old style C program and run it successfully under C++. Java appears to be similar to C++ when we consider only the “extension” part of C++. However, some object-oriented features of C++ make the C++ code extremely difficult to follow and maintain.

Listed below are some major C++ features that were intentionally omitted from Java or significantly modified.

  • Java does not support operator overloading.
  • Java does not have template classes as in C++.
  • Java does not support multiple inheritance of classes. This is accomplished using a new feature called “interface”.
  • Java does not support global variables. Every variable and method is declared within a class and forms part of that class.
  • Java does not use pointers.
  • Java has replaced the destructor function with a finalize () function.
  • There are no header files in Java.

 

Below are the differences specified in tabular form :

 

C+

 

Java

 

C++ is not a purely object-oriented programming, since it is possible to write C++ programs without using a class or an object.

 

Java is purely an object-oriented programming language, since it is not possible to write a Java program without using at least one class.

 

Pointers are available in C++.

 

We cannot create and use pointers in Java.

 

Allocating memory and de-allocating memory is the responsibility of the programmer.

 

Allocation and de-allocation of memory will be taken care of by JVM.

 

C++ has goto statement.

 

Java does not have goto statement.

 

Automatic casting is available in C++.

 

In some cases, implicit casting is available. But it is advisable that the programmer should use casting wherever possible.

 

Multiple inheritance feature is available in C++.

 

No multiple inheritance in Java, but there are means to achieve it.

 

Operator overloading is available in C++.

 

It is not available in Java.

 

#define, typedef and header files are available in C++.

 

#define, typedef and header are not available in Java, but there are means to achieve them.

 

There are 3 access specifiers in C++: private, public and protected.

 

Java supports 4 access specifiers: private, public, protected, and default.

 

There are constructors and destructors in C++.

 

Only constructors are there in Java. No destructors are available in this language.

 

Java also adds some new features. While C++ is a superset of C, Java is neither a superset nor a subset of C or C++. Java may be considered as a first cousin of C++ and a second cousin of C as illustrated in the below figure.


Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud