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
Creating an Application
Although Java has become well-known because it can be used in conjunction with World Wide Web pages, you can also use it to write any type of computer program. The Saluton program you wrote during Hour 2, "Writing Your First Program," is an example of a Java application.
To try out another program, use your word processor to open up a new file and enter everything from Listing 4.1. Remember not to enter the line numbers and colons along the left side of the listing; these items are used to make parts of programs easier to describe in the book. When you're done, save the file as Root.java, making sure to save it in text-only or plain ASCII text format.
Example 4.1. The Full Text of Root.java
1: class Root {
2: public static void main(String[] arguments) {
3: int number = 225;
4: System.out.println("The square root of "
5: + number
6: + " is "
7: + Math.sqrt(number) );
8: }
9: }
The Root application accomplishes the following tasks:
- Line 3: An integer value of 225 is stored in a variable named number.
- Lines 4–7: This integer and its square root are displayed. The Math.sqrt(number) statement in Line 7 displays the square root.
Before you can test out this application, you need to compile it using the Software Development Kit's javac compiler or the compiler included with another Java development environment. If you're using the SDK, go to a command line, open the folder that contains the Root.java file, then compile Root.java by entering the following at a command line:
javac Root.java
If you have entered Listing 4.1 without any typos, including all punctuation and every word capitalized exactly as shown, it should compile without any errors. The compiler responds to a successful compilation by not responding with any message at all.
Java applications are compiled into a class file that can be run by a Java interpreter. If you're using the SDK, you can run the compiled Root.class file by typing this command:
java Root
The output should resemble the following:
The square root of 225 is 15.0
When you run a Java application, the interpreter looks for a main() block and starts handling Java statements at that point. If your program does not have a main() block, the interpreter will respond with an error.
Sending Arguments to Applications | Next Section

Account Sign In
View your cart