Chapter 5: Controlling Program Flow (Kids web site) with Decision-Making Statements

Chapter 5: Controlling Program Flow with Decision-Making Statements 113 Importing everything in one fell swoop The first line of Listing 5-3 illustrates a lazy way of importing both System. outand System.in. To import everything that Systemhas to offer, you use the asterisk wildcard character (*). In fact, importing java.lang.System.* is like having about 30 separate importdeclarations, including System.in, System.out, System.err, System.nanoTime, and many other System things. The use of an asterisk in an importdeclaration is generally considered bad programming practice, so I don t do it often in this book s examples. But for larger programs programs that use dozens of names from the Java API the lazy asterisk trick is handy. You can t toss an asterisk anywhere you want inside an importdeclaration. For example, you can t import everything starting with java by writing import java.*. You can substitute an asterisk only for the name of a class or for the name of something static that s tucked away inside a class. For more information about asterisks in import declarations, see Chapter 9. For information about static things, see Chapter 10. Java s logical operators Mr. Spock would be pleased. Java has all the operators that you need for mixing and matching logical tests. The operators are shown in Table 5-2. Table 5-2 Logical Operators Operator Symbol Meaning Example && and 5 < x && x < 10 || or x < 5 || 10 < x ! not !password.equals( swordfish ) You can use these operators to form all kinds of elaborate conditions. Listing 5-4 has an example. Some runs of the program of Listing 5-4 are shown in Figure 5-5. When the username is bburd and the password is swordfish or when the username is hritter and the password is preakston, the user gets a nice message. Otherwise, the user is a bum who gets the nasty message that he or she deserves.
Would you like to be a member of headache free web site owner’s community. Now you can. We focus in IX Web Hosting, go go go!

Leave a Reply