My web site - 128 Introduction to Java Applets Chapter 3 quires
128 Introduction to Java Applets Chapter 3 quires to do its task. The Graphics object uses this data to draw the String at the specified location. The identifiers number1, number2 and sum are the names of variables. A variable is similar to an object. The primary difference between a variable and an object is that an object is defined by a class definition that can contain both data (instance variables) and methods, whereas a variable is defined by a primitive (or built-in) data type (one of char, byte, short, int, long, float, doubleor boolean) that can contain only data. A variable can store exactly one value at a time, whereas one object may contain many individual pieces of data. The distinction between a variable and a reference is based on the data type of the identifier, which is stated in a declaration. If the data type is a class name, the identifier is a reference to an object and that reference can be used to send messages to (call methods on) that object. If the data type is one of the primitive data types, the identifier is a variable that can be used to store in memory or retrieve from memory a single value of the declared primitive type. Software Engineering Observation 3.8 A hint to help you determine if an identifier is a variable or a reference is the variable s data type. By convention all class names in Java start with a capital letter. Therefore, if the data type starts with a capital letter, normally you can assume that the identifier is a reference to an object of the declared type (e.g., Graphics g indicates that g is a reference to a Graphics object). Lines 22 23 // obtain first number from user firstNumber = JOptionPane.showInputDialog( “Enter first floating-point value” ); read the first floating-point number from the user. JOptionPane method showInput- Dialog displays an input dialog that prompts the user to enter a value. The user types a value in the input dialog s text field, then clicks the OK button to return the string the user typed to the program. If you type and nothing appears in the text field, position the mouse pointer in the text field and click the mouse to make the text field active. Variable first- Numberis assigned the result of the call to JOptionPane.showInputDialog operation with an assignment statement. The statement is read as firstNumber gets the value of JOptionPane.showInputDialog(”Enterfirstfloating-point value”). In lines 22 23, notice the method call syntax. At this point, we have seen two different ways to call methods. This statement uses the static method call syntax introduced in Chapter 2. All static methods are called with the syntax ClassName.methodName( arguments ) Also in this chapter, we have called methods of class Graphicswith a similar syntax that started with a reference to a Graphics object. Generically, this syntax is referenceName.methodName( arguments ) This syntax is used for most methods calls in Java. In fact, the applet container uses this syntax to call methods init, start and paint on your applets. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01