****
This section describes Java’s facilities for getting access to info about the machine on which the Java virtual machine is really running.
1.2 Command-Line Arguments
This is very similar to C++. The Java code:
public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
can be compared to the C++:
int main (int nArgs, char** args) {
for (int i = 0; i < nArgs; ++i) {
cout << args[i] << end;
}
return 0;
}
The main function in C++ gets two arguments instead of one. The first argument indicates how many command line parameters are supplied in the args
array. Java does not need this info to be passed spearately, because Java arrays “know” their own length. The main content in both languages is an array of strings. In C++ these are “old-fashioned” C-strings (char*
) with the second asterisk indicating that this is an array.
One difference worth noting however. In C++, position zero of the array contains the name of the program as supplied on the command line. Hence, if we run the programs, the first line of output will be different:
> java Echo I am here
I
am
here
> ./cppProgramName I am here
./cppProgramName
I
am
here
© 2014-2022, Old Dominion Univ.