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
Q&A
-
The if statement seems like the one that's most useful. Is it possible to use only if statements in programs and never use the others?
It's possible to do without else or switch, and many programmers never use the ternary operator ?. However, else and switch often are beneficial to use in your programs because they make them easier to understand. A set of if statements chained together can become unwieldy.
-
An if statement is described as either a single statement or as a conditional statement followed by another statement to handle if the condition is true. Which is it?
The point that might be confusing is that if statements and other conditionals are used in conjunction with other statements. The if statement makes a decision, and the other statements do work based on the decision that is made. The if statement combines a conditional statement with one or more other types of Java statements, such as statements that use the println() method or create a variable.
-
In the ClockTalk program, why is 1 added to Calendar.MONTH to get the current month value?
This is necessary because of a quirk in the way that the Calendar class represents months. Instead of numbering them from 1 to 12 as you might expect, Calendar numbers months beginning with 0 in January and ending with 11 in December. Adding 1 causes months to be represented numerically in a more understandable manner.
-
During this hour, opening and closing brackets { and } are not used with an if statement if it is used in conjunction with only one statement. Isn't it mandatory to use brackets?
No. Brackets can be used as part of any if statement to surround the part of the program that's dependent on the conditional test. Using brackets is a good practice to get into because it prevents a common error that might take place when you revise the program. If you add a second statement after an if conditional and don't add brackets, unexpected errors will occur when the program is run.
-
Will the SDK's Java compiler catch the error when a = operator is used with a conditional instead of a == ?
Often it will not, and it results in a real doozy of a logic error. These errors only show up when a program is being run and can be discovered only through observation and testing. Because the = operator is used to assign a value to a variable, if you use name = "Fernando" in a spot in a program where you mean to use name == "Fernando", you could wipe out the value of the name variable and replace it with Fernando. When the value stored in a variable changes unexpectedly, it can cause subtle and unexpected errors.
-
Does break have to be used in each section of statements that follow a case ?
You don't have to use break. If you do not use it at the end of a group of statements, all of the remaining statements inside the switch block statement will be handled, regardless of the case value they are being tested with.
-
Why do you sometimes use single-quotation marks after a case statement and sometimes leave them off?
The value associated with a case statement must be either a character or an integer. The single-quotation marks surround a char value and are not used with an int value, so they differentiate between these two data types—for example, case '1': looks for the '1' character and case 1: looks for the integer value of 1.
Quiz | Next Section

Account Sign In
View your cart