Thinking in java 4th edition error found

Source: Internet
Author: User

Thinking in java has read more than five or six times, but some chapters have not been carefully read before. I recently started to review thinking in java due to my own lack of technical skills. I found a mistake in the book on the 491 page of the original book.

 

1: //: generics/Wildcards. java
2: // processing the meaning of wildcards.
3:
4: public class Wildcards {
5: // Raw argument:
6: static void rawArgs (Holder holder, Object arg ){
7: // holder. set (arg); // Warning:
8: // Unchecked call to set (T) as
9: // member of the raw type Holder
10: // holder. set (new Wildcards (); // Same warning
11:
12: // Can't do this; don't have any 'T ':
13: // T t = holder. get ();
14:
15: // OK, but type information has been lost:
16: Object obj = holder. get ();
17 :}
18: // Similar to rawArgs (), but errors instead of warnings:
19: static void unboundedArg (Holder <?> Holder, Object arg ){
20: // holder. set (arg); // Error:
21: // set (capture ?) In Holder <capture of?>
22: // cannot be applied to (Object)
23: // holder. set (new Wildcards (); // Same error
24:
25: // Can't do this; don't have any 'T ':
26: // T t = holder. get ();
27:
28: // OK, but type information has been lost:
29: Object obj = holder. get ();
30 :}
31: static <T> T exact1 (Holder <T> holder ){
32: T t = holder. get ();
33: return t;
34 :}
35: static <T> T exact2 (Holder <T> holder, T arg ){
36: holder. set (arg );
37: T t = holder. get ();
38: return t;
39 :}
40: static <T>
41: T wildSubtype (Holder <? Extends T> holder, T arg ){
42: // holder. set (arg); // Error:
43: // set (capture? Extends T) in
44: // Holder <capture? Extends T>
45: // cannot be applied to (T)
46: T t = holder. get ();
47: return t;
48 :}
49: static <T>
50: void wildSupertype (Holder <? Super T> holder, T arg ){
51: holder. set (arg );
52: // T t = holder. get (); // Error:
53: // Incompatible types: found Object, required T
54:
55: // OK, but type information has been lost:
56: Object obj = holder. get ();
57 :}
58: public static void main (String [] args ){
59: Holder raw = new Holder <Long> ();
60: // Or:
61: raw = new Holder ();
62: Holder <Long> qualified = new Holder <Long> ();
63: Holder <?> Unbounded = new Holder <Long> ();
64: Holder <? Extends Long> bounded = new Holder <Long> ();
65: Long lng = 1L;
66:
67: rawArgs (raw, lng );
68: rawArgs (qualified, lng );
69: rawArgs (unbounded, lng );
70: rawArgs (bounded, lng );
71:
72: unboundedArg (raw, lng );
73: unboundedArg (qualified, lng );
74: unboundedArg (unbounded, lng );
75: unboundedArg (bounded, lng );
76:
77: // Object r1 = exact1 (raw); // Warnings:
78: // Unchecked conversion from Holder to Holder <T>
79: // Unchecked method invocation: exact1 (Holder <T>)
80: // is applied to (Holder)
81: Long r2 = exact1 (qualified );
82: Object r3 = exact1 (unbounded); // Must return Object
83: Long r4 = exact1 (bounded );
84:
85: // Long r5 = exact2 (raw, lng); // Warnings:
86: // Unchecked conversion from Holder to Holder <Long>
87: // Unchecked method invocation: exact2 (Holder <T>, T)
88: // is applied to (Holder, Long)
89: Long r6 = exact2 (qualified, lng );
90: // Long r7 = exact2 (unbounded, lng); // Error:
91: // exact2 (Holder <T>, T) cannot be applied
92: // (Holder <capture of?>, Long)
93: // Long r8 = exact2 (bounded, lng); // Error:
94: // exact2 (Holder <T>, T) cannot be applied
95: // to (Holder <capture? Extends Long>, Long)
96:
97: // Long r9 = wildSubtype (raw, lng); // Warnings:
98: // Unchecked conversion from Holder
99: // to Holder <? Extends Long>
100: // Unchecked method invocation:
101: // wildSubtype (Holder <? Extends T>, T) is
102: // applied to (Holder, Long)
103: Long r10 = wildSubtype (qualified, lng );
104: // OK, but can only return Object:
105: Object r11 = wildSubtype (unbounded, lng );
106: Long r12 = wildSubtype (bounded, lng );
107:
108: // wildSupertype (raw, lng); // Warnings:
109: // Unchecked conversion from Holder
110: // to Holder <? Super Long>
111: // Unchecked method invocation:
112: // wildSupertype (Holder <? Super T>, T)
113: // is applied to (Holder, Long)
114: wildSupertype (qualified, lng );
115: // wildSupertype (unbounded, lng); // Error:
116: // wildSupertype (Holder <? Super T>, T) cannot be
117: // applied to (Holder <capture of?>, Long)
118: // wildSupertype (bounded, lng); // Error:
119: // wildSupertype (Holder <? Super T>, T) cannot be
120: // applied to (Holder <capture? Extends Long>, Long)
121 :}
122 :}///:~
See row 104-105:

// OK, but can only return Object:
Object r11 = wildSubtype (unbounded, lng );

Holder <?> Yes, it cannot be passed to Holder <? Extends T>.

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.