Tuesday, 24 February 2009

Java™ How to Program, Sixth Edition - Part18

Few weeks back i started reading Java™ How to Program, Sixth Edition By H. M. Deitel - Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates, Inc.

Nice book written By H. M. Deitel - Deitel & Associates, Inc., P. J. Deitel - Deitel & Associates, Inc.

I wanted to share few quotations found the book from the 18th chapter.

1) Generic methods enable programmers to specify, with a single method declaration, a set of related methods.

2) Generic classes enable programmers to specify, with a single class declaration, a set of related types.

3) Generic methods and classes are among Java's most powerful capabilities for software reuse with compile-time type safety.

4) Overloaded methods are often used to perform similar operations on different types of data.

5) When the compiler encounters a method call, it always attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call.

6) If the operations performed by several overloaded methods are identical for each argument type, the overloaded methods can be more compactly and conveniently coded using a generic method. You can write a single generic method declaration, which can be called with arguments of different data types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

7) All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type.

8) Each type parameter section contains one or more type parameters (also called formal type parameters) separated by commas.

9) A type parameter, also known as type variable, is an identifier that specifies a generic type name. The type parameters can be used as the return type, parameter types and local variable types in a generic method declaration, and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. Type parameters can represent only reference types.

10) The names used for type parameters throughout the method declaration must match those declared in the type parameter section. The name of a type parameter can be declared only once in the type parameter section but can appear more than once in the method's parameter list. Type parameter names need not be unique among different generic methods.

11) When the compiler encounters a method call, it first determines the argument types and attempts to locate a method with the same name that specifies parameters that match the argument types. If there is no such method, the compiler determines whether there is an inexact but applicable match.

12) The relational operator > cannot be used with reference types. However, it is possible to compare two objects of the same class if that class implements the generic interface Comparable (package java.lang).

13) Comparable objects have a compareTo method that must return 0 if the objects are equal, -1 if the first object is less than the second or 1 if the first object is greater than the second.

14) All the type-wrapper classes for primitive types implement Comparable.

15) A benefit of implementing interface Comparable is that Comparable objects can be used with the sorting and searching methods of class Collections (package java.util).

16) When the compiler translates a generic method into Java bytecodes, it removes the type parameter section and replaces the type parameters with actual types. This process is known as erasure. By default each type parameter is replaced with its upper bound. By default, the upper bound is type Object unless specified otherwise in the type parameter section.

17) When the compiler performs erasure on a method that returns a type variable, it also inserts explicit cast operations in front of each method call to ensure that the returned value is of the type expected by the caller.

18) A generic method may be overloaded. A class can provide two or more generic methods that specify the same method name but different method parameters. A generic method can also be overloaded by non-generic methods that have the same method name and number of parameters. When the compiler encounters a method call, it searches for the method declaration that most precisely matches the method name and the argument types specified in the call.

19) When the compiler encounters a method call, it performs a matching process to determine which method to call. The compiler tries to find and use a precise match in which the method names and argument types of the method call match those of a specific method declaration. If this fails, the compiler determines whether a generic method is available that provides a precise match of the method name and argument types, and if so, uses that generic method.

20) Generic classes provide a means for describing a class in a type-independent manner. We can then instantiate type-specific objects of the generic class.

21) A generic class declaration looks like a non-generic class declaration, except that the class name is followed by a type parameter section. As with generic methods, the type parameter section of a generic class can have one or more type parameters separated by commas.

22) When a generic class is compiled, the compiler performs erasure on the class's type parameters and replaces them with their upper bounds.

23) Type parameters cannot be used in a class's static declarations.

24) When instantiating an object of a generic class, the types specified in angle brackets after the class name are known as type arguments. They are used by the compiler to replace the type parameters so that the compiler can perform type checking and insert cast operations as necessary.

25) It is possible to instantiate a generic class without specifying a type argument. In this case, the new object of the class is said to have a raw type, which means that the compiler implicitly uses type Object (or the type parameter's upper bound) throughout the generic class for each type argument.

26) The Java Collections Framework provides many generic data structures and algorithms that manipulate the elements of those data structures. Perhaps the simplest of the data structures is class ArrayList dynamically resizable, array-like data structure.

27) Class Number is the superclass of both Integer and Double.

28) Method add of class ArrayList appends an element to the end of the collection.

29) Method toString of class ArrayList returns a string of the form"[ elements ]" in which elements is a comma-separated list of the elements' string representations.

30) Method doubleValue of class Number obtains the Number's underlying primitive value as a double value.

31) Wildcard type arguments enable you to specify method parameters, return values, variables, etc. that act as supertypes of parameterized types. A wildcard type argument is denoted by the question mark (?), which represents an "unknown type." A wildcard can also have an upper bound.

32) Because a wildcard (?) is not a type parameter name, you cannot use it as a type name throughout a method's body.

33) If a wildcard is specified without an upper bound, then only the methods of type Object can be invoked on values of the wildcard type.

34) Methods that use wildcards as type arguments cannot be used to add elements to a collection referenced by the parameter.

35) A generic class can be derived from a non-generic class. For example, Object is a direct or indirect superclass of every generic class.

36) A generic class can be derived from another generic class.

37) A non-generic class can be derived from a generic class.

38) A generic method in a subclass can override a generic method in a superclass if both methods have the same signatures.

About the Authors

Dr. Harvey M. Deitel, Chairman and Chief Strategy Officer of Deitel & Associates, Inc., has 43 years experience in the computing field, including extensive industry and academic experience. Dr. Deitel earned B.S. and M.S. degrees from the Massachusetts Institute of Technology and a Ph.D. from Boston University. He worked on the pioneering virtual-memory operating-systems projects at IBM and MIT that developed techniques now widely implemented in systems such as UNIX, Linux and Windows XP. He has 20 years of college teaching experience, including earning tenure and serving as the Chairman of the Computer Science Department at Boston College before founding Deitel & Associates, Inc., with his son, Paul J. Deitel. He and Paul are the co-authors of several dozen books and multimedia packages and they are writing many more. With translations published in Japanese, German, Russian, Spanish, Traditional Chinese, Simplified Chinese, Korean, French, Polish, Italian, Portuguese, Greek, Urdu and Turkish, the Deitels' texts have earned international recognition. Dr. Deitel has delivered hundreds of professional seminars to major corporations, academic institutions, government organizations and the military.

Paul J. Deitel, CEO and Chief Technical Officer of Deitel & Associates, Inc., is a graduate of the MIT's Sloan School of Management, where he studied Information Technology. Through Deitel & Associates, Inc., he has delivered Java, C, C++, Internet and World Wide Web courses to industry clients, including IBM, Sun Microsystems, Dell, Lucent Technologies, Fidelity, NASA at the Kennedy Space Center, the National Severe Storm Laboratory, Compaq, White Sands Missile Range, Rogue Wave Software, Boeing, Stratus, Cambridge Technology Partners, Open Environment Corporation, One Wave, Hyperion Software, Adra Systems, Entergy, CableData Systems and many other organizations. Paul is one of the most experienced Java corporate trainers having taught about 100 professional Java training courses. He has also lectured on C++ and Java for the Boston Chapter of the Association for Computing Machinery. He and his father, Dr. Harvey M. Deitel, are the world's best-selling Computer Science textbook authors.

No comments: