74 Introduction to (Web hosting ecommerce) Java Applications Chapter 2 which

74 Introduction to Java Applications Chapter 2 which uses the operator +to add a String(the literal “Thesumis “) and the value of variable sum(the intvariable containing the result of the addition on line 31). Java has a version of the +operator for string concatenation that concatenates a Stringand a value of another data type (including another String); the result of this operation is a new (and normally longer) String. If we assume that sumcontains the integer value 117, the expression evaluates as follows: 1. Java determines that the two operands of the + operator (the string “The sum is “and the integer sum) are of different types and one of them is a String. 2. Java converts sumto a String. 3. Java appends the Stringrepresentation of sumto the end of “Thesumis “, resulting in the String”Thesumis117″. Method showMessageDialog displays the resulting String in the dialog box. Note that the automatic conversion of integer sum occurs only because the addition operation concatenates the String literal “The sum is” and sum. Also, note that the space between isand 117is part of the string “Thesumis “. String concatenation is discussed in detail in Chapter 10, Strings and Characters. Common Programming Error 2.10 Confusing the +operator used for string concatenation with the +operator used for addition can lead to strange results. For example, assuming that integer variable yhas the value 5, the expression “y+2=”+y+2results in the string “y+2=52″, not “y+2=7″, because first the value of yis concatenated with the string “y+2=”, and then the value 2 is concatenated with the new larger string “y+2=5″. The expression “y+2=”+(y+ 2)produces the desired result. The third and fourth arguments of method showMessageDialogin Fig. 2.9 represent the string that should appear in the dialog box s title bar and the dialog box type, respectively. The fourth argument JOptionPane.PLAIN_MESSAGE is a value indicating the type of message dialog to display. This type of message dialog does not display an icon to the left of the message. Figure 2.11 illustrates the second and third arguments and shows that there is no icon in the window. The message dialog types are shown in Fig. 2.12. All message dialog types except PLAIN_MESSAGEdialogs display an icon to the user indicating the type of message. The user clicks OK to dismiss the dialog. Argument 2: The message to display Argument 3: The title bar string Fig. 2.111 Message dialog box customized with the four-argument version of Fig. method showMessageDialog. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply