13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.2 A Simple Start101public static void main(String args[])From within main(), the various parameters are available as the elementsof the array of Strings. The class in Example 4.1 will display those parameterswhen the program is run.Example 4.1 Java program to dump command-line arguments/** simple command-line parameter displayer*/public classCLine{public static voidmain(String [] args){for (int i = 0; i < args.length; i++){System.out.println(args[i]);}} // main} // class CLineWe compile and run the example, providing a few command-lineparameters:$ javac CLine.java$ java CLine hello world file.txt bluehelloworldfile.txtblue$Not all classes will have main() methods, but any can. Even if severalclasses in a package have main() methods, that is not a problem. Which <strong>on</strong>ewill be the “main” main()? It’s the class we specified when we invoked ourprogram. In Example 4.1, the main() that is executed is the <strong>on</strong>e in the CLine

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!