Java The Complete Reference, Seventh Edition
As explained, you can specify the message displayed when an assertion fails. For example, if you substitute assert n > 0 : "n ...
if(n < 0) { System.out.println("n is negative!"); return; // or throw an exception } Withassert, you need only one line of co ...
side1 = 3.0; side2 = 4.0; // Notice how sqrt() and pow() must be qualified by // their class name, which is Math. hypot = Math.s ...
In this version, the namessqrtandpoware brought into view by these static import statements: import static java.lang.Math.sqrt; ...
As convenient as static import can be, it is important not to abuse it. Remember, the reason that Java organizes its libraries i ...
Chapter 13: I/O, Applets, and Other Topics 313 This class contains three constructors, each of which initializes the values ofaa ...
However, you need to be careful. Constructors that callthis( )will execute a bit slower than those that contain all of their ini ...
14 Generics 14 Generics S ince the original 1.0 release in 1995, many new features have been added to Java. The one that has had ...
316 Part I: The Java Language What Are Generics? At its core, the termgenericsmeansparameterized types.Parameterized types are i ...
Chapter 14: Generics 317 // Demonstrate the generic class. class GenDemo { public static void main(String args[]) { // Create a ...
Next,Tis used to declare an object calledob, as shown here: T ob; // declare an object of type T As explained,Tis a placeholder ...
The next line assigns toiOba reference to an instance of anIntegerversion of theGen class: iOb = new Gen(88); Notice that when t ...
Generics Work Only with Objects When declaring an instance of a generic type, the type argument passed to the type parameter mus ...
} // Show type of ob. void showType() { System.out.println("Type of ob is " + ob.getClass().getName()); } } // Demonstrate the n ...
322 Part I: The Java Language Notice this line: int v = (Integer) iOb.getob(); Because the return type ofgetob( )isObject, the c ...
Chapter 14: Generics 323 // Show types of T and V. void showTypes() { System.out.println("Type of T is " + ob1.getClass().getNam ...
324 Part I: The Java Language In this case,Integeris substituted forT, andStringis substituted forV. Although the two type argum ...
for(int i=0; i < nums.length; i++) sum += nums[i].doubleValue(); // Error!!! return sum / nums.length; } } InStats, theaverag ...
326 Part I: The Java Language // Demonstrate Stats. class BoundsDemo { public static void main(String args[]) { Integer inums[] ...
Chapter 14: Generics 327 Here,Tis bounded by a class calledMyClassand an interface calledMyInterface. Thus, any type argument pa ...
«
13
14
15
16
17
18
19
20
21
22
»
Free download pdf