Page 331
Tuesday, November 21st, 2006Page 332
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider
Page 332
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider
Page 331
Hint: If you are looking for very good and affordable webspace to host and run your tomcat hosting application check Virtualwebstudio tomcat web hosting provider
Page 329
Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services
Page 330
Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services
Page 328
Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services
[ Team LiB ] 6.6 Assigning, Passing, and Casting Reference Values Reference values, like primitive values, can be assigned, cast, and passed as arguments. For values of the primitive data types and reference types, conversions can occur during . assignment . parameter passing . explicit casting The rule of thumb for the primitive data types is that widening conversions are permitted, but narrowing conversions require an explicit cast. The rule of thumb for reference values is that conversions up the type hierarchy are permitted (upcasting), but conversions down the hierarchy require explicit casting (downcasting). In other words, conversions that are from a subtype to its supertypes are allowed, other conversions require an explicit cast or are illegal. There is no notion of promotion for reference values. Reference Value Assignment Conversions Reference value assignments are generally permitted up the type hierarchy, with implicit conversion of the source reference value to that of the destination reference type. Example 6.11 Assigning and Passing Reference Values interface IStack { /* From Example 6.9 */ } interface ISafeStack extends IStack { /* From Example 6.9 */ } class StackImpl implements IStack { /* From Example 6.9 */ } class SafeStackImpl extends StackImpl implements ISafeStack { /* From Example 6.9 */ } public class ReferenceConversion { public static void main(String[] args) { // Reference declarations Object objRef; StackImpl stackRef; SafeStackImpl safeStackRef; IStack iStackRef; ISafeStack iSafeStackRef; // SourceType is a class type safeStackRef = new SafeStackImpl(10); objRef = safeStackRef;// (1) Always possible stackRef = safeStackRef;// (2) Subclass to superclass assignment iStackRef = stackRef; // (3) StackImpl implements IStack iSafeStackRef = safeStackRef;// (4) SafeStackImpl implements ISafeStack // SourceType is an interface type objRef = iStackRef; // (5) Always possible iStackRef = iSafeStackRef; // (6) Sub- to super-interface assignment // SourceType is an array type. Object[] objArray = new Object[3]; StackImpl[] stackArray = new StackImpl[3]; SafeStackImpl[] safeStackArray = new SafeStackImpl[5]; Page 327
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services
[ Team LiB ] 6.5 Completing the Type Hierarchy Table 6.1 summarizes the types found in Java. Only primitive data and reference values can be stored in variables. Only class and array types can be instantiated to create objects. Table 6.1. Types and Values Types Values Primitive data types Primitive data values Class, interface, and array types (reference types) Reference values Arrays are objects in Java. Array types (boolean[], Object[], StackImpl[]) implicitly augment the inheritance hierarchy. The inheritance hierarchy depicted in Figure 6.3 can be augmented by the corresponding array types. The resulting type hierarchy is shown in Figure 6.4. An array type is shown as a “class” with the [] notation appended to the name of the element type. The class SafeStackImpl is a subclass of the class StackImpl. The corresponding array types, SafeStackImpl[] and StackImpl[], are shown as subtype and supertype, respectively, in the type hierarchy. Figure 6.4 also shows array types corresponding to some of the primitive data types. Figure 6.4. Reference Type Hierarchy From the type hierarchy in Figure 6.4, we can summarize the following: . All reference types are subtypes of Object type. This applies to classes, interfaces, and array types, as these comprise all reference types. . All arrays of reference types are also subtypes of the array type Object[], but arrays of primitive data types are not. Note that the array type Object[] is also a subtype of Object type. . If a reference type is a subtype of another reference type, then the corresponding array types also have an analogous subtype-supertype relationship. . There is no subtype-supertype relationship between a type and its corresponding array Page 325
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services
[ Team LiB ] Page 326
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check Actions tomcat hosting services
[ Team LiB ] Page 324
Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services
Page 323
Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services