Appendix B

Differences Between java and C++


CONTENTS


The Java language is very similar to the C and C++ languages, from which it is derived. These similarities make Java an easy language to learn for those who have previously programmed in C++, but they also mask some of the important differences between Java and C++. This appendix summarizes these differences.

Program Structure

The following sections identify the differences between the way Java and C++ programs are structured.

The main() Method

Every Java program contains a public class with a main() method that serves as the entry point for that program. A typical Java program often references other Java classes that are included within the same package as the program's class or in other compiled packages.

C++ programs also make uses of a main() entry point. The C++ analog of the Java method is referred to as a member function, or as just a function. The C++ main() function takes two parameters: the int argc variable that identifies the number of arguments passed to the invoked program and the argv[][] character array that contains the program arguments. The Java main() method takes a single args[] parameter of the String class. The number of arguments passed via the main() method invocation is determined by args.length.

The first value passed as the argument to a C++ program is the name of the program. Subsequent arguments are used to pass other values. Java programs do not pass the program name as the first argument to the program and are therefore off by one position with respect to the arguments passed to C++ programs.

The main() function of a C++ program has an int return type by default. This is used to return an exit code. The Java main() method has a void return type. The exit() method of the java.lang.System class can be used to return an exit code.

Packages

All Java classes are defined relative to a package, even if it is the default noname package. C++ programs do not support a package approach.

Importing Classes

Java programs reference classes that are defined in other packages using the import statement. C++ classes that are declared outside of a compilation unit are referenced using #include compiler directives.

Functions and Variables Declared Outside of Classes

C++ allows functions and variables to be defined outside the scope of a class. In fact, C++ does not require any classes to be defined.

Java strictly adheres to a class-oriented approach to program design. It is impossible to define a method or variable outside the scope of a class. At least one public class must be defined within a Java program to support the main() method.

Program Development

The following sections identify differences between the way Java and C++ programs are developed.

Compilation and Execution

C++ programs are generally compiled into native machine object code and executed directly as a process running under the local operating system.

Java programs are compiled into the bytecode instructions of the Java virtual machine and are executed using the Java interpreter or a Java-compatible browser.

The Preprocessor and Compiler Directives

C++ source code files are processed by a preprocessor before they are submitted to the compiler. Java does not use a preprocessor.

C++ provides the capability to communicate with the C++ compiler using compiler directives. Java does not provide any similar capability.

The Java API Versus C++ Libraries

The Java API provides a rich set of classes that can be used to support program development. These classes are largely incompatible with standard C and C++ libraries. Existing C and C++ code can be made available to Java programs using native methods.

Using CLASSPATH

Java uses the CLASSPATH environment variable to identify Java classes. Other environment variables are used by C and C++ programs.

Language Syntax

The following sections identify significant distinctions between the syntax of Java and C++.

Comments

Java and C++ use the same style of comments. Java also provides the capability to insert doc comments, which are processed by the javadoc program to support automated program documentation.

Constants

C++ defines constants using the #DEFINE compiler directive and the const keyword. Java constants are identified using the final keyword.

Primitive Data Types

Java provides the boolean, byte, short, char, int, long, float, and double primitive data types. C++ supports the same data types but does not use exactly the same type names as Java. Java's char data type uses a 16-bit character value to represent Unicode characters. C++ uses 8-bit ASCII char values.

Keywords

Java and C++ each identify different sets of reserved keywords, although some keywords are reserved by both Java and C++.

Java Classes Versus C++ Classes

Java classes are declared using a similar, but different, syntax than C++ classes.

C++ allows functions to be separately prototyped from their actual implementation. Java does not allow separate function prototyping.

C++ allows class definitions to be nested. Java does not.

C++ classes support multiple inheritance. Java classes support only single inheritance. Java uses interfaces to implement certain features of multiple inheritance. C++ does not support an analog to the Java interface. Objective-C does provide similar support.

C++ supports templates. Java does not.

Public, Private, and Protected Keywords

The access keywords used by Java and C++ have the same names, but they are used slightly differently. Java access keywords are defined relative to the package in which a class, interface, variable, or method is defined. C++ access keywords are defined relative to the class, data member, and member functions to which they apply.

Variables

A Java variable either contains the value of a primitive data type or refers to a Java object. Java objects are instances of classes or arrays.

C++ variables are not restricted in the same manner as Java variables. They may refer to primitive data types, instances of classes, arrays, structs, unions, or other data structures.

Types

The types supported by Java are the primitive data types, classes, interfaces, and arrays. C++ supports a variety of types, including primitive types, classes, structs, unions, and defined types. The C++ typedef construct does not have an analog in Java. C++ also supports enumerated data types, and Java does not. C++ is much more flexible in providing implicit casting between types. Java supports C++-style casting operators, but does not support implicit casting to the same extent as C++.

Pointers

C++ allows pointers to other objects to be defined. Java does not support pointers. All variables that do not contain the values of primitive data types are references to an object. This reference value may only be changed as the result of an assignment to a new object. The reference value may not be directly tampered with or manipulated as is possible with C++ pointers.

Objects

Java objects are instances of classes or arrays. C++ objects do not include arrays.

Java objects are created using the new operator and are deallocated automatically via the Java garbage collector. C++ objects can be statically or dynamically allocated. Static allocation is accomplished using a type declaration. Dynamic allocation is accomplished using the new operator. C++ variables must be explicitly deallocated using a destructor or the delete operator.

Arrays

Java arrays are similar to C++ arrays. However, there are some significant differences between the two languages.

C++ supports true multidimensional arrays. Java supports single-dimensional arrays; it simulates multidimensional arrays as arrays of arrays. The approach used by Java is actually more flexible than that of C++. Java arrays can consist of arrays of different dimensions.

Java arrays are objects and inherit the methods of the Object class. C++ arrays are not objects in their own right.

Java arrays are allocated using the new operator and are deallocated when they are garbage-collected. Java arrays are separately declared and allocated, although both steps may occur in the same statement. Java arrays can be statically initialized using the same syntax as C++ arrays. However, C++ requires static initializers to be constant expressions, but Java does not.

Java arrays are declared with a more flexible syntax than C++ arrays. In particular, the brackets used in Java array declarations may be associated with the name of the array being declared or the type of array being declared.

Strings

The Java String and StringBuffer classes are used to implement text strings. These classes allow strings to be treated as objects. C++ implements strings as null-terminated arrays of characters.

null Versus NULL

The Java null keyword is used to identify a variable as not referencing any object. The null keyword cannot be assigned to a variable that is declared with a primitive data type. The C++ NULL value is a constant that is defined as 0.

Statements

The syntax of Java statements is nearly identical to that of C++. However, Java does not support a goto statement, whereas C++ does. Java does, however, reserve the goto keyword.

The Java if, while, and do statements require that the conditional expression used to determine the flow of control results in a boolean value. C++ does not place this restriction on these statements.

Java supports labeled break and continue statements to break out of and continue executing complex switch and loop constructs.

Java provides the synchronized statement to support multithreading operations on critical sections. C++ does not support a synchronized statement.

Java implements exception handling using the try statement and the catch and finally clauses. C++ implements exception handling in a similar manner, but does not support a finally clause.

Methods Versus Functions

The C++ analog of the Java method is the member function. C++ functions are more powerful and flexible than Java methods. C++ functions allow variable-length argument lists and optional arguments. Java does not support these capabilities.

C++ allows inline functions to be specified and Java does not. C++ implements friend functions to circumvent the normal class access restrictions. Java does not support friend functions.

Operators

The set of operators supported by Java is based on those provided by C++, although not all C++ operators are supported. In addition, Java provides operators that are not supported by C++. These include the instanceof operator and the + operator used with String objects.

C++ supports operator overloading and Java does not.