The invocation System.out.println( … ) in line (D) is a call to the println() method that
is defined for the output stream object out. More precisely, out is a field of type OutputStream
defined in the class System. System is a class that comes with the java. lang package.
[9]
This
package is loaded in automatically by the Java compiler. println() is a method defined for the
class OutputStream. One could also use the method print() via the invocation System.
out.print( … ) if it is not necessary to display the output in a separate line of text. The
println and the print methods are as defined for the PrintStream class. The argument to
these methods must either be a string or a type that Java would know how to convert into a string
for display. In our example, the second part of the argument, of type int, gets converted into a
string automatically.
The rest of the program consists of the method addArray() in line (E), which is very much like the
C++ function of the same name in the earlier program, except for the manner in which the size of
the array is determined inside the function. For both C and C++, the size of the array had to be
passed explicitly to the function. But in Java, that is not necessary. Arrays in Java are objects that
have data members
[10]
associated with them. The data member that is associated with an array
object is length. When we access this data member through the call data.length, we can
determine the length of the array data.
Also note from the above examples that C++ and Java have exactly the same way of commenting
code. You can either use the C-style comment delimiters /* …. */ or //. However, the latter can only
be used for comments on a single line, because the compiler will not see any characters past //.
With regard to comments in Java programs, a special tool called javadoc can automatically
generate documentation for your program using text that is delimited by /** and */. This tool
generates HTML files that can be viewed with a browser.
[1]
It is important to bear in mind that while an array name can "decay" into a pointer, it is not a
pointer. The extent to which an array name decays into a pointer depends, among other things, on
whether an array name is the name of a parameter of a callable function. So, whereas the array
name data in main will act like a pointer in some contexts only, the array name a in the function
addArray of the program AddArray1. c will act like a pointer in practically all contexts.
[2]
The C++ Standard Library consists of these header files: algorithm, bitset, complex,
deque, exception, fstream, functional, iomanip, ios, iosfwd, iostream,
istream, iterator, limits, list, locale, map, memory, new, numeric,
ostream, queue, set, sstream, stack, stdexcept, streambuf, string,
typeinfo, utility, valarray, vector. Of these, the following are informally referred to
as the Standard Template Library (STL): algorithm, bitset, deque, functional,
iterator, list, map, queue, set, stack, valarray and vector.
[3]
Operator overloading, discussed in detail in Chapter 12, allows the same operator to be used in
different ways. The operands determine as to which meaning of such an operator applies in a given
context.
[4]
You do not need to specify the classpath for the classes that come with the Java platform. Both
the compiler and the application launcher can locate those automatically.
[5]
By directory name here is meant the pathname to the directory.
[6]
If you are using a Cygnus emulation of Unix on Windows, you may need to place the classpath
string between double quotes.
[7]
A JAR file in Java is an archive file, just like a Unix tar (tape archive) file. Jar files are created and
manipulated by using the Java jar tool. To create a JAR archive of all your classes, including the
sources, in your current directory, you'd say