Major Differences Between Java and C++


Memory Management

  1. Java has no explicit memory reclamation operators--garbage collection used instead.

  2. Java has no explicit pointers

  3. Java eliminates value (i.e., stack-allocated) objects--all objects are allocated off the heap.


Project Management

  1. Java has a module mechanism called packages that groups related classes together. Classes in the same package typically can directly access one another's instance variables. Java's package mechanism eliminates the need for C++'s kludgy friends operator.

  2. Java has no .h files. All the declarations and code for a class are placed in the same .java file. Java's designers made this decision because they felt it was confusing to have to keep switching back and forth between .h and .cpp files to determine how a class operates.

  3. Java does not require a make file. The compiler is supposed to find all the necessary .java files using a CLASSPATH environment variable (discussed later in the course) and then compile the .java files if they are out of date.


Inheritance

  1. Java does not support multiple inheritance because the developers of Java felt that both the conceptual and implementation complexities and ambiguities it introduces outweigh the benefits. Java instead uses the notion of interfaces for classes that want to support more than one set of methods. Interfaces will be discussed later in the course.

  2. All methods in Java are virtual methods.


Other Language Differences

  1. Functions cannot exist outside objects--even main is part of an object.

  2. The original Java language had no templates. It obtained its genericity by using a master class, Object, that can be downcasted. Java 1.5 has added generic data structures and you are free to use either the template facility or the old Object class to obtain genericity.

  3. Java does not have formatted I/O although it uses C++'s notion of a stream. Java does provide special formatting classes, like a DecimalFormat class, that allow various primitive types to be formatted. The reason for providing formatting objects is to simplify the internationalization of software. The formatting objects will return different representations of an object depending on the region in the world where the software is executing. For example, the DecimalFormat class formats floating point numbers using decimal points in the US and using commas in Europe. This type of formatting is more flexible than the hardcoded formatting that is done in C++ programs, although it is also more complicated to use.

  4. The original Java did not have enumerated types. You instead declare integer constants, which are declared using the final keyword rather than the const keyword. Java 1.5 added enumerated types and you may use them if you wish.


Environment Issues

  1. Java does not compile its programs down to machine-level instructions as is done by C++. Instead it compiles programs into byte codes that are interpreted by a virtual machine. A virtual machine is a software program that simulates a machine and its instruction set.

  2. Because of its byte codes, Java is architecture-neutral. In other words, a Java byte code program will run on any machine on which its virtual machine runs. This does mean that the virtual machine must be specially coded for each architectural platform.

  3. Java's virtual machine provides a good deal of run-time checking to ensure that the program does not breach the security of the platform on which the program is running:

  4. The interpretation and runtime services come at the price of decreased efficiency. Even well coded Java programs often run two times slower than corresponding C++ programs. Just-in-time (JIT) compilers can compile byte codes down to machine instructions but the resulting programs are still less efficient than corresponding C++ programs because: 1) the programs still must provide the run-time services, and 2) a compiler can optimize a program better when it has access to the original source code than to the byte codes.

  5. Java does not create a single monolithic object file like C++ does. Instead it dynamically loads classes as they are requested by the program. This means that a program sent over the Internet can start executing before all of its classes have arrived.