Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
Sending Arguments to Applications
Because Java applications are run from a command line, you can send information to applications at the same time you run them. The following example uses the java interpreter to run an application called DisplayTextFile.class, and it sends two extra items of information to the application, readme.txt and /p:
java DisplayTextFile readme.txt /p
Extra information you can send to a program is called an argument. The first argument, if there is one, is provided one space after the name of the application. Each additional argument is also separated by a space.
If you want to include a space inside an argument, you must put quotation marks around the argument, as in the following:
java DisplayTextFile readme.txt /p "Page Title"
This example runs the DisplayTextFile program with three arguments: readme.txt, /p, and Page Title. The quote marks prevent Page and Title from being treated as separate arguments.
You can send as many arguments as you want to a Java application. In order to do something with them, however, you have to write some statements in the application to handle them.
To see how arguments work in an application, create a new file in your word processor called Blanks.java. Enter the text of Listing 4.2 into the file and save it when you're done. Compile the program, correcting any errors that are caused by typos.
Example 4.2. The Full Text of Blanks.java
1: class Blanks {
2: public static void main(String[] arguments) {
3: System.out.println("The " + arguments[0]
4: + " " + arguments[1] + " fox "
5: + "jumped over the "
6: + arguments[2] + " dog.");
7: }
8: }
To try out the Blanks application, run it with a Java interpreter such as the SDK's java tool. Give it three adjectives of your own choosing as arguments, as in the following example:
java Blanks retromingent purple lactose-intolerant
The application uses the adjectives to fill out a sentence. Here's the one produced by the preceding three arguments:
The retromingent purple fox jumped over the lactose-intolerant dog.
Try it with some of your own adjectives, making sure to always include at least three of them.
Arguments are a useful way to customize the performance of a program. They are often used to configure a program so it runs a specific way. Java stores arguments in arrays, groups of related variables that all hold the same information. You'll learn about arrays during Hour 9, "Storing Information with Arrays."
Applet Basics | Next Section

Account Sign In
View your cart