Chapter 6 Methods 259 Converting values to lower (Photoshop web design)

Chapter 6 Methods 259 Converting values to lower types can result in different values. Therefore, in cases where information may be lost due to conversion, the Java compiler requires the programmer to use a cast operator to force the conversion to occur. To invoke our square method, which uses an integer parameter with the doublevariable y (Fig. 6.3), we write the method call as square((int)y). This method call explicitly casts (converts) the value of y to an integer for use in method square. Thus, if y s value is 4.5, method squarereturns 16, not 20.25. Common Programming Error 6.11 Converting a primitive-data-type value to another primitive data type may change the value if the new data type is not an allowed promotion (e.g., double to int). Also, converting any integral value to a floating-point value and back to an integral value may introduce rounding errors into the result. 6.7 Java API Packages As we have seen, Java contains many predefined classes that are grouped into categories of related classes, called packages. Together, we refer to these packages as the Java applications programming interface (Java API), or the Java class library. Throughout the text, importstatements specify the classes required to compile a Java program. For example, a program uses the statement import javax.swing.JApplet; to tell the compiler to load the JAppletclass from the javax.swingpackage. One of the great strengths of Java is the large number of classes in the packages of the Java API that programmers can reuse rather than reinventing the wheel. We exercise a large number of these classes in this book. Figure 6.6 lists a subset of the many packages in the Java API and provides a brief description of each package. We use classes from these packages and others throughout this book. We provide this table to begin introducing you the variety of reusable components available in the Java API. When learning Java, you should spend time reading the descriptions of the packages and classes in the Java API documentation (java.sun.com/j2se/1.3/docs/api). Package Description java.applet The Java Applet Package. This package contains the Appletclass and several interfaces that enable the creation of applets, interaction of applets with the browser and playing audio clips. In Java 2, class javax.swing.JApplet is used to define an applet that uses the Swing GUI components. java.awt The Java Abstract Windowing Toolkit Package. This package contains the classes and interfaces required to create and manipulate graphical user interfaces in Java 1.0 and 1.1. In Java 2, these classes can still be used, but the Swing GUI components of the javax.swingpackages are often used instead. Fig. 6.6Packages of the Java API (part 1 of 2). Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply