There are a number of additional differences between Java and C++ beyond those already mentioned in this chapter. This section will briefly describe some additional differences.
Comments
In addition to the // and /*…*/ comments of C++, Java introduces a new comment delimiter. Java's new comment delimiter begins with /** and ends with */. A comment that is enclosed within these delimiters can be extracted from the source code and used to create documentation for the class with the JavaDoc utility.
Command-Line Arguments
In a C or C++ program, the program is passed command-line arguments in the familiar argc and argv parameters to the program's main function. These parameters represent a count of the number of parameters and an array of parameter values, respectively. There will always be at least one parameter passed to a C or C++ program because the first parameter is the program name.
In a Java application, the command-line arguments are based in an array of String objects. The signature for main is as follows:
public static void main(String args[]);
Each component of the array args is one of the command-line arguments. A difference between C++ and Java is that the program name is not passed to the program as the first command-line argument in Java. Consider two programs that are invoked in the following manner:
For C++: program 100 200For Java: java program 100 200
The command lines of these two programs will be interpreted by C++ and Java as shown in Table 4.3.
Character Arrays and Strings
Because Java does not allow direct manipulation of pointers, it does not support C-style, null-terminated strings. For its string support, Java utilizes a String class. Although it is possible to allocate an array of type char to emulate a C++ string, the two types are not the same.
goto, break, and continue
You probably won't shed any tears, but the goto statement is not part of Java. On the other hand, it is still part of the reserved word list so it may come back at any time. Java does replace goto, however, with the ability to use break and continue with labels. You can still use break and continue as you are used to from C++, but you can now use them to pass control flow in other ways. For example, consider the following code:
int line=1;outsideLoop:for(int out=0; out<3; out++) {for(int inner=0;inner < 5; inner++) {if (foo(inner) < 10))break outsideLoop;else if (foo(inner) > 100)continue outsideLoop;}}
In this example, if the foo method returns a value less than 10, the code break outsideLoop will execute. A normal break here would break out of the inner loop. However, because this is a named break statement, it will break out of the named outer loop. This example also demonstrates the use of continue with a label.
Runtime Type Identification
Runtime Type Identification (RTTI) is a relatively new feature to C++, or at least to many C++ compilers. A form of RTTI is included in Java through its instanceof keyword.
Missing Features
As you begin programming in Java, you will notice that it is missing a couple of other aspects of C++ that you may use. In most cases, there is either no need for the feature in Java, due to its simplified model for object-oriented programming, or there is an alternative way of accomplishing the goal.
Features left out of Java that you may rely on in C++ include templates, name spaces, friend functions, and default parameters. In many cases, you will find that these old friends are no longer necessary in Java, or, at least, that their removal is justified by how much doing so simplifies the rest of your code.
No comments:
Post a Comment