String[] args

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page
strArray[0] = "Good-by" ;

Answer:

This will replace the reference in cell 0 with a reference to a new String, containing the characters "Good-by" .

String[] args

Each cell of an array of object references works just like an ordinary object reference variable. In the question, strArray[0] starts out with a reference to one String, then is assigned a reference to another. The first String is now garbage.

Here is the familiar signature of the main() method:

public static void main( String[] args )

The phrase String[] args says that main() has a parameter which is an array of String references. This array is constructed by the Java system just before main() gets control. The elements of the array refer to Strings that contain text from the command line that starts the program. For example, say that a program is started with this command line:

C:/>java StringDemo stringA stringB stringC

The picture shows what the parameter args looks like while main() is running.

QUESTION 9:

What would the following statement print?

System.out.println( args[0] );