Major Differences Between Java and C++
Memory Management
- Java has no explicit memory reclamation operators--garbage collection
used instead.
- Java has no explicit pointers
- Java eliminates value (i.e., stack-allocated) objects--all objects
are allocated off the heap.
Project Management
- 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.
- 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.
- Typically you will only declare one class per file.
- The class name must match the file name
- 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
- 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.
- All methods in Java are virtual methods.
Other Language Differences
- Functions cannot exist outside objects--even main is part of
an object.
- 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.
- 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.
- 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
- 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.
- 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.
- 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:
- array bounds checking
- type checking of downcasts
- garbage collection so that the programmer does not have to handle
memory deallocation and dangling pointers
- 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.
- 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.