Major Differences Between Java and C++
- Java has no explicit memory reclamation operators--garbage collection
- Java has no explicit pointers
- Java eliminates value (i.e., stack-allocated) objects--all objects
are allocated off the heap.
- 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
and namespace mechanism.
- 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.
- 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
- All methods in Java are virtual methods.
Other Language Differences
- Functions cannot exist outside objects--even main is part of
- Java does not have formatted input although it uses C++'s notion of
a stream. You can use a scanner class to break an input line into
tokens. Java has both a formatted output statement similar to C++ and
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.
- Text-oriented I/O can seem clunkier in Java than in C/C++ because Java
was designed to work with graphical interfaces, and hence do
graphical I/O. When Java became popular as a mainstream computing
language, the developers added in text-oriented I/O.
- 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