Ads 468x60px

Monday, July 25, 2011

How Java Differs from C++ ?


 Now that you have a basic understanding of creating and working with classes, objects, and interfaces, you should be ready for Chapter 2, "Getting Started," that introduces you to the Java development environment. Before proceeding to Chapter 2, read through this section; it explains the differences between Java and C++. It is important that you understand what Java has changed and the reasons for these changes.
Java functionality differs from that of C and C++ in many ways. As I discussed earlier in this chapter, these changes are intended to create an object-oriented language that eliminates many of the opportunities for bugs and memory leaks that are common in C and C++. If you have experience in C or C++ programming, some of Java's changes may take some getting used to. The following list touches on the most important of these changes:
  • Java is an interpreted language, not a compiled language as is C++. This means that compiling is done by an interpreter before execution.
  • Java uses classes or interfaces to build composite data types instead of structures and unions, as in C++. This ensures portability.
  • There are no #defines in Java because the development team felt that using #defines advocates coding that is hard to read.
  • Command-line arguments are different in Java. They are arrays of strings that contain the arguments. Through a mechanism known as varargs, C++ allows you to provide a variable number of arguments to a function. This mechanism is not supported by the Java language.
  • Java has no header files. Instead, Java uses interfaces that show only the methods and final, or constant, variables instead of the entire structure.
  • Pointers, one of the primary features that introduce bugs and memory leaks into programs, are removed in Java. By getting rid of structures and encapsulating arrays through references, Java has attempted to get rid of the original reasoning behind pointers. Java does not allow you to construct a reference to anonymous memory, so it produces robust, efficient code much less prone to bugs, memory leaks, and corruption.
  • Java has replaced multiple inheritance by interfaces to avoid problems with fragile superclasses.
  • To ensure a purely object-oriented structure, there are no individual functions in Java. Functions must be encapsulated in a class.
  • While Java retains goto as a reserved word, it is not implemented or supported by the Java language.
  • Java has strict definition of operators. It doesn't allow for operator overloading.
  • Automatic coercion, which is a common cause of inaccuracy in C++, would allow you to place an incompatible variable into another without declaring that you were aware of this change. In Java, in order to store a variable of one type in a variable of another type, you must explicitly call it with a cast statement.
  • Java programs crash reliably and obviously, whereas crashes in C and C++ programs are not as apparent.
  • Java implements a new function called automatic garbage collection. The Java runtime system keeps track of all references to an object until the object is no longer needed. When there are no more references to an object, it makes it available for garbage collection.
  • Java also implements automatic memory management and thread controls. Although threads still require the use of classes, Java balances the addition of thread synchronization between the language and class levels. For example, garbage collection is run as a background process (or low-priority thread). It remains quiet until there is either a sufficient pause in the execution of foreground threads for it to run, or the system explicitly requires the use of memory which is taken up by unreferenced classes.
  • The Java language provides a finally statement for use with Java exceptions. The finally statement delimits a block of code used to release system resources and perform various other cleanup operations after the try statement.
  • Java strings are first-class objects. They are a class provided in the java.lang package. This provides consistency and predictability in string functions. 

0 comments:

Post a Comment