5: Controlling Program Flow with Decision-Making Statements in Java
5: Controlling Program Flow with Decision-Making Statements Drawing Code-39 In Java Using Barcode creator for Java Control to generate, create Code 39 Extended image in Java applications. string To ask the second question (the more appropriate question) Java s String type has a method named equals (Like everything else in the known universe, this equals method is defined in the Java API, short for Application Programming Interface) The equals method compares two strings to see whether they have the same characters in them For an example using Java s equals method, see Listing 5-3 (A run of the program in Listing 5-3 is shown in Figure 5-4) Painting Barcode In Java Using Barcode encoder for Java Control to generate, create bar code image in Java applications. Listing 5-3: Read Barcode In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. Checking a Password
ANSI/AIM Code 39 Creator In C# Using Barcode drawer for .NET framework Control to generate, create Code 39 Extended image in .NET applications. import static javalangSystem*; import javautilScanner; class CheckPassword { public static void main(String args[]) { outprint( What s the password ); Scanner myScanner = new Scanner(in); String password = myScannernext(); outprintln( You typed >> + password + << ); outprintln(); if (password == swordfish ) { outprintln( The word you typed is stored ); outprintln( in the same place as the real ); outprintln( password You must be a ); outprintln( hacker ); } else { outprintln( The word you typed is not ); outprintln( stored in the same place as ); outprintln( the real password, but that s ); outprintln( no big deal ); } outprintln(); if (passwordequals( swordfish )) { outprintln( The word you typed has the ); outprintln( same characters as the real ); outprintln( password You can use our ); outprintln( precious system ); } else { outprintln( The word you typed doesn t ); outprintln( have the same characters as ); outprintln( the real password You can t ); outprintln( use our precious system ); } } } Code 39 Extended Maker In .NET Using Barcode creation for Visual Studio .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications. Part II: Writing Your Own Java Programs
Code 39 Extended Generation In Visual Basic .NET Using Barcode maker for Visual Studio .NET Control to generate, create Code 3 of 9 image in VS .NET applications. Figure 5-4: The result of using == and using Java s equals method
Data Matrix Creator In Java Using Barcode creation for Java Control to generate, create ECC200 image in Java applications. In Listing 5-3, the call myScannernext() grabs whatever word the user types on the computer keyboard The code shoves this word into the variable named password Then the program s if statements use two different techniques to compare password with swordfish The more appropriate of the two techniques uses Java s equals method The equals method looks funny because when you call it you put a dot after one string and put the other string in parentheses But that s the way you have to do it In calling Java s equals method, it doesn t matter which string gets the dot and which gets the parentheses For instance, in Listing 5-3, you could have written if ( swordfish equals(password)) The method would work just as well A call to Java s equals method looks imbalanced, but it s not There s a reason behind the apparent imbalance between the dot and the parentheses The idea is that you have two objects: the password object and the swordfish object Each of these two objects is of type String (However, password is a variable of type String, and swordfish is a String literal) When you write passwordequals( swordfish ), you re calling an equals method that belongs to the password object As you call that method, you re feeding swordfish to the method as the method s parameter (pun intended) You can read more about methods belonging to objects in 7 When comparing strings with one another, use the equals method not the double equal sign Making Code-39 In Java Using Barcode drawer for Java Control to generate, create Code 3 of 9 image in Java applications. 5: Controlling Program Flow with Decision-Making Statements
Painting Bar Code In Java Using Barcode encoder for Java Control to generate, create barcode image in Java applications. Importing everything in one fell swoop
Encode GTIN - 13 In Java Using Barcode printer for Java Control to generate, create EAN13 image in Java applications. The first line of Listing 5-3 illustrates a lazy way of importing both System out and Systemin To import everything that System has to offer, you use the asterisk wildcard character (*) In fact, importing javalangSystem* is like having about 30 separate import declarations, including Systemin, Systemout, Systemerr, SystemnanoTime, and many other System things The use of an asterisk in an import declaration 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 import declaration 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 9 For information about static things, see 10 Encode Barcode In Java Using Barcode creation for Java Control to generate, create barcode image in Java applications. International Standard Serial Number Creator In Java Using Barcode creator for Java Control to generate, create ISSN - 10 image in Java applications. Making Barcode In .NET Framework Using Barcode generator for .NET Control to generate, create barcode image in VS .NET applications. Bar Code Generation In C# Using Barcode drawer for .NET Control to generate, create bar code image in .NET framework applications. Barcode Scanner In Visual Studio .NET Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications. Generating Data Matrix 2d Barcode In Visual C#.NET Using Barcode generation for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications. |
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |