Knowledge (XXG)

Generics in Java

Source 📝

2931:. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown. 114:
is generic if it declares one or more type variables. It defines one or more type variables that act as parameters. A generic class declaration defines a set of parameterized types, one for each possible invocation of the type parameter section. All of these parameterized types share the same class
140:
can be declared as generic, independently of whether the class that the constructor is declared in is itself generic. A constructor is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the constructor. The form of the formal type
124:
is generic if it declares one or more type variables. It defines one or more type variables that act as parameters. A generic interface declaration defines a set of types, one for each possible invocation of the type parameter section. All parameterized types share the same interface at
3121:
Objects with generic type in Java are non-reifiable due to type erasure. Java only enforces type information at compile-time. After the type information is verified at compile-time, the type information is discarded, and at run-time, the type information will not be available.
3077:, the Java compiler will (correctly) throw a compile-time exception to indicate the presence of incompatible types (since generics are invariant). Hence, this avoids potential run-time exceptions. This problem can be fixed by creating an instance of 277:) when executing the third line of code. This type of logic error can be detected during compile time by using generics and is the primary motivation for using them. It defines one or more type variables that act as parameters. 3043:. This is a benefit of using generic when compared to non-generic objects such as arrays. Specifically, generics can help prevent run time exceptions by throwing a compile-time exception to force the developer to fix the code. 132:
is generic if it declares one or more type variables. These type variables are known as the formal type parameters of the method. The form of the formal type parameter list is identical to a type parameter list of a class or
1924:
A type argument for a parameterized type is not limited to a concrete class or interface. Java allows the use of "type wildcards" to serve as type arguments for parameterized types. Wildcards are type arguments in the form
3117:
More formally speaking, objects with generic type in Java are non-reifiable types. A non-reifiable type is type whose representation at run-time has less information than its representation at compile-time.
3002:
are shared among all the instances of the class, regardless of their type parameter. Consequently, the type parameter cannot be used in the declaration of static variables or in static methods.
107:
is an unqualified identifier. Type variables are introduced by generic class declarations, generic interface declarations, generic method declarations, and by generic constructor declarations.
2578:. Many people are dissatisfied with this restriction. There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an 3054:
object, no compile-time exception is thrown (since arrays are covariant). This may give the false impression that the code is correctly written. However, if the developer attempts to add a
1933:. Given that the exact type represented by a wildcard is unknown, restrictions are placed on the type of methods that may be called on an object that uses parameterized types. 2103:
The use of wildcards above adds flexibility since there is not any inheritance relationship between any two parameterized types with concrete type as type argument. Neither
3148:
is an experimental project to incubate improved Java generics and language features, for future versions potentially from Java 10 onwards. Potential enhancements include:
3040: 3034: 2444: 2440: 1416: 3564: 2049:
method would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is
3377: 81:
created Generic Java, an extension to the Java language to support generic types. Generic Java was incorporated in Java with the addition of
3559: 3540: 1295:
in the above method, we will get compilation error (cannot find symbol "Type"), since it represents the declaration of the symbol.
3145: 3415: 3413: 3411: 3409: 2563:, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code uses the correct type. 42: 455: 3186: 3158: 3111: 3105: 3022: 406:
constituents). With generics, it is no longer necessary to cast the third line to any particular type, because the result of
2551:
Generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called
568:
Here is an example of a generic Java class, which can be used to represent individual entries (key to value mappings) in a
3191: 153: 49:
to allow "a type or method to operate on objects of various types while providing compile-time type safety". The aspect
447: 3018: 38: 161: 3114:, meaning that an array object enforces its type information at run-time, whereas generics in Java are not reified. 2924:
Due to type erasure, the runtime will not know which catch block to execute, so this is prohibited by the compiler.
439: 63: 3223: 150:
The following block of Java code illustrates a problem that exists when not using generics. First, it declares an
3005:
Type erasure was implemented in Java to maintain backward compatibility with programs written prior to Java SE5.
3176: 2135:
into it; which violates type safety. Here is an example that demonstrates how type safety would be violated if
51: 3392: 3356: 3501: 1586:) for a pair of angle brackets containing the one or more type parameters that a sufficiently close context 2057: 1930: 569: 2368:
keyword is used. This keyword indicates that the type argument is a supertype of the bounding class. So,
3478: 3436: 1298:
In many cases, the user of the method need not indicate the type parameters, as they can be inferred:
3326: 3324: 3322: 3320: 3318: 3316: 3314: 3312: 3299: 3297: 3295: 3293: 3291: 2570:
is examined at runtime, there is no general way to determine whether, before type erasure, it was an
2226:// valid if List<Integer> were a subtype of List<Number> according to substitution rule. 56: 3289: 3287: 3285: 3283: 3281: 3279: 3277: 3275: 3273: 3271: 2455:
Although exceptions themselves cannot be generic, generic parameters can appear in a throws clause:
3458: 3262: 3171: 3152: 34: 2282:
The solution with wildcards works because it disallows operations that would violate type safety:
3210: 2566:
Because of type erasure, type parameters cannot be determined at run-time. For example, when an
182:—an error in logic, as it is not generally possible to cast an arbitrary string to an integer. 3536: 3017:
arrays), and generics in Java. Two of the major differences, namely, differences in terms of
120: 3422:, pp. 139–145, Chapter §5 Item 31: Use bounded wildcards to increase API flexibility. 3181: 2999: 2928: 2066:
keyword is used to indicate that the type argument is a subtype of the bounding class. So
1919: 1587: 1578:, Java SE 7 and above allow the programmer to substitute an empty pair of angle brackets ( 82: 27:
Form of abstraction for a type or method to allow them to be functions of a type parameter
1575: 1412: 74: 3553: 141:
parameter list is identical to a type parameter list of a generic class or interface.
103: 78: 3066:. This run-time exception can be completely avoided if the developer uses generics. 3432: 2552: 2436: 2425: 2050: 1456:
There is also the possibility to create generic methods based on given parameters.
418: 70: 3013:
There are several important differences between arrays (both primitive arrays and
2084:
means that the given list contains objects of some unknown type which extends the
66:
supports generics to specify the type of objects stored in a collection instance.
46: 55:
required that parametrically polymorphic functions are not implemented in the
3265:
by James Gosling, Bill Joy, Guy Steele, Gilad Bracha – Prentice Hall PTR 2005
17: 2045:, it stands for some unknown type. Any method argument value we pass to the 273:
Although the code is compiled without error, it throws a runtime exception (
3251: 417:
The logical flaw in the third line of this fragment will be detected as a
3221:
A ClassCastException can be thrown even in the absence of casts or nulls.
2744:
Another effect of type erasure is that a generic class cannot extend the
2439:
gives an easy way to remember when to use wildcards (corresponding to
1191:
Here is an example of a generic method using the generic class above:
3242:, pp. 123–125, Chapter §5 Item 27: Eliminate unchecked warnings. 813:
This generic class could be used in the following ways, for example:
421:
error (with J2SE 5.0 or later) because the compiler will detect that
280:
The above code fragment can be rewritten using generics as follows:
2431:
The mnemonic PECS (Producer Extends, Consumer Super) from the book
3445:...The sole exception is null, which is a member of every type... 3073:
object an creates a new instance of this object with return type
3306:, pp. 126–129, Chapter §5 Item 28: Prefer lists to arrays. 3085:
object instead. For code using Java SE7 or later versions, the
3345:, pp. 135–138, Chapter §5 Item 30: Favor generic methods. 2605:
Demonstrating this point, the following code outputs "Equal":
436:
Here is a small excerpt from the definition of the interfaces
2998:
Because there is only one copy per generic class at runtime,
2779:
The reason why this is not supported is due to type erasure:
3333:, pp. 117–122, Chapter §5 Item 26: Don't use raw types. 2590:(however, it may have been parameterized with any parent of 2364:
To specify the lower bounding class of a type wildcard, the
1511:
In such cases you can't use primitive types either, e.g.:
2416:. Adding to such a list requires either elements of type 2934:
For example, the following code cannot be compiled:
2041:
generic interface. When the actual type argument is
3134: 3130: 3126: 3090: 3086: 3082: 3078: 3074: 3070: 3063: 3059: 3055: 3051: 3047: 3014: 2395: 2369: 2067: 2061: 2586:, that ArrayList may have been parameterized with 1350:The parameters can be explicitly added if needed: 2096:. Reading an element from the list will return a 3035:Covariance and contravariance (computer science) 2100:. Adding null elements is, again, also allowed. 1183:grade: (Mike, A) mark: (Mike, 100) 13 is prime. 59:, since type safety is impossible in this case. 2277:// now 3.14 is assigned to an Integer variable! 1936:Here is an example where the element type of a 433:. For a more elaborate example, see reference. 3378:"Type Inference for Generic Instance Creation" 2127:. If it did, it would be possible to insert a 2123:as a parameter does not accept an argument of 2029:stands for, we cannot add objects to it. The 2025:Since we don't know what the element type of 226:// A String that cannot be cast to an Integer 174:. Finally, it attempts to retrieve the added 8: 3533:"Effective Java: Programming Language Guide" 3479:"Java Language Specification, Section 8.1.2" 3050:object and instantiates the object as a new 3393:"Generics in the Java Programming Language" 3357:"Generics in the Java Programming Language" 3224:"Java and Scala's Type Systems are Unsound" 3161:; making actual types available at runtime. 3039:Generics are invariant, whereas arrays are 3263:Java Language Specification, Third Edition 2748:class in any way, directly or indirectly: 2559:will be converted to the non-generic type 1451:// Fails compilation. Use Integer instead. 3029:Covariance, contravariance and invariance 45:5.0. They were designed to extend Java's 3125:Examples of non-reifiable types include 3046:For example, if a developer declares an 3203: 2111:is a subtype of the other; even though 414:by the code generated by the compiler. 390:within the angle brackets declares the 381:// (type error) compilation-time error 2088:class. For example, the list could be 3419: 3342: 3330: 3303: 3239: 3094: 1929:"; optionally with an upper or lower 1590:. Thus, the above code example using 7: 3437:"Wildcards > Bonus > Generics" 2428:(which is a member of every type). 2053:; which is a member of every type. 3535:(third ed.). Addison-Wesley. 3062:object, the program will throw an 25: 2394:. Reading from a list defined as 3137:is a generic formal parameter. 1940:is parameterized by a wildcard: 3565:Polymorphism (computer science) 2033:method takes arguments of type 1419:versions must be used instead: 3106:Reification (computer science) 1: 3391:Gilad Bracha (July 5, 2004). 3355:Gilad Bracha (July 5, 2004). 1291:Note: If we remove the first 3089:can be instantiated with an 3069:If the developer declares a 2119:. So, any method that takes 275:java.lang.ClassCastException 89:Hierarchy and classification 3560:Java (programming language) 3459:"Reified Generics for Java" 3457:Gafter, Neal (2006-11-05). 95:Java Language Specification 3581: 3192:Comparison of Java and C++ 3103: 3032: 2927:Java generics differ from 2547:Problems with type erasure 2037:, the element type of the 1917: 1905:" is not prime." 1187:Generic method definitions 1169:" is not prime." 64:Java collections framework 3211:Java Programming Language 3187:Comparison of C# and Java 2451:Generics in throws clause 2412:returns elements of type 564:Generic class definitions 39:Java programming language 3177:Template metaprogramming 3087:Collection<Object> 3079:Collection<Object> 3071:Collection<Object> 2990://causes a compile error 2936: 2781: 2750: 2607: 2572:ArrayList<Integer> 2457: 2284: 2145: 2060:of a type wildcard, the 1942: 1596: 1513: 1458: 1421: 1352: 1300: 1193: 815: 574: 462: 282: 184: 52:compile-time type safety 3083:ArrayList<Object> 3009:Differences from Arrays 77:, David Stoutamire and 41:in 2004 within version 37:that were added to the 3531:Bloch, Joshua (2018). 3502:"Welcome to Valhalla!" 3155:, e.g. List<int> 3153:generic specialization 2951:instantiateElementType 2576:ArrayList<Float> 1863:" is prime." 1121:" is prime." 3075:ArrayList<Long> 2338:// compile-time error 1999:// compile-time error 1594:can be rewritten as: 398:(a descendant of the 394:to be constituted of 3506:OpenJDK mail archive 2712:// evaluates to true 1415:is not allowed, and 57:Java virtual machine 3441:The Java™ Tutorials 3172:Generic programming 3141:Project on generics 3064:ArrayStoreException 2859:"Integer" 2557:List<Integer> 2137:List<Integer> 2125:List<Integer> 2109:List<Integer> 2039:Collection<E> 1938:Collection<E> 1719:"grade: " 962:"grade: " 386:The type parameter 35:generic programming 3131:List<String> 2913:"String" 2481:throwMeConditional 2392:List<Object> 2388:List<Number> 2141:List<Number> 2139:were a subtype of 2121:List<Number> 2105:List<Number> 2094:List<Number> 1749:"mark: " 992:"mark: " 448:java.util.Iterator 178:and cast it to an 166:. Then, it adds a 33:are a facility of 3093:object using the 3091:ArrayList<> 2733:"Equal" 2420:, any subtype of 2090:List<Float> 1403:"Hello" 1342:"Hello" 268:// Run time error 16:(Redirected from 3572: 3546: 3518: 3517: 3515: 3513: 3497: 3491: 3490: 3488: 3486: 3475: 3469: 3468: 3466: 3465: 3454: 3448: 3447: 3429: 3423: 3417: 3404: 3403: 3397: 3388: 3382: 3381: 3374: 3368: 3367: 3361: 3352: 3346: 3340: 3334: 3328: 3307: 3301: 3266: 3260: 3254: 3252:GJ: Generic Java 3249: 3243: 3237: 3231: 3230: 3228: 3219: 3213: 3208: 3159:reified generics 3146:Project Valhalla 3136: 3132: 3128: 3095:diamond operator 3092: 3088: 3084: 3080: 3076: 3072: 3065: 3061: 3057: 3053: 3049: 3016: 3000:static variables 2994: 2991: 2988: 2985: 2982: 2979: 2976: 2973: 2970: 2967: 2964: 2961: 2958: 2955: 2952: 2949: 2946: 2943: 2940: 2920: 2917: 2914: 2911: 2908: 2905: 2902: 2899: 2896: 2893: 2890: 2887: 2884: 2881: 2878: 2875: 2874:GenericException 2872: 2869: 2866: 2863: 2860: 2857: 2854: 2851: 2848: 2845: 2842: 2839: 2836: 2833: 2830: 2827: 2824: 2821: 2820:GenericException 2818: 2815: 2812: 2809: 2806: 2803: 2800: 2797: 2796:GenericException 2794: 2791: 2788: 2785: 2775: 2772: 2769: 2766: 2763: 2760: 2759:GenericException 2757: 2754: 2747: 2740: 2737: 2734: 2731: 2728: 2725: 2722: 2719: 2716: 2713: 2710: 2707: 2704: 2701: 2698: 2695: 2692: 2689: 2686: 2683: 2680: 2677: 2674: 2671: 2668: 2665: 2662: 2659: 2656: 2653: 2650: 2647: 2644: 2641: 2638: 2635: 2632: 2629: 2626: 2623: 2620: 2617: 2614: 2611: 2601: 2597: 2593: 2589: 2585: 2581: 2577: 2573: 2569: 2562: 2558: 2542: 2539: 2536: 2533: 2530: 2527: 2524: 2521: 2518: 2515: 2512: 2509: 2506: 2503: 2500: 2497: 2494: 2491: 2488: 2485: 2482: 2479: 2476: 2473: 2470: 2467: 2464: 2461: 2423: 2419: 2415: 2411: 2410: 2407: 2404: 2401: 2398: 2393: 2389: 2386:could represent 2385: 2384: 2381: 2378: 2375: 2372: 2367: 2360: 2357: 2354: 2351: 2348: 2345: 2342: 2339: 2336: 2333: 2330: 2327: 2324: 2321: 2318: 2315: 2312: 2309: 2306: 2303: 2300: 2297: 2294: 2291: 2288: 2278: 2275: 2272: 2269: 2266: 2263: 2260: 2257: 2254: 2251: 2248: 2245: 2242: 2239: 2236: 2233: 2230: 2227: 2224: 2221: 2218: 2215: 2212: 2209: 2206: 2203: 2200: 2197: 2194: 2191: 2188: 2185: 2182: 2179: 2176: 2173: 2170: 2167: 2164: 2161: 2158: 2155: 2152: 2149: 2142: 2138: 2134: 2130: 2126: 2122: 2118: 2115:is a subtype of 2114: 2110: 2106: 2099: 2095: 2091: 2087: 2083: 2082: 2079: 2076: 2073: 2070: 2065: 2064: 2048: 2044: 2040: 2036: 2032: 2028: 2021: 2018: 2015: 2012: 2009: 2006: 2003: 2000: 1997: 1994: 1991: 1988: 1985: 1982: 1979: 1976: 1973: 1970: 1967: 1964: 1961: 1958: 1955: 1952: 1949: 1946: 1939: 1928: 1909: 1906: 1903: 1900: 1897: 1894: 1891: 1888: 1885: 1882: 1879: 1876: 1873: 1870: 1867: 1864: 1861: 1858: 1855: 1852: 1849: 1846: 1843: 1840: 1837: 1834: 1831: 1828: 1825: 1822: 1819: 1816: 1813: 1810: 1807: 1804: 1801: 1798: 1795: 1792: 1789: 1786: 1783: 1780: 1777: 1774: 1771: 1768: 1765: 1762: 1759: 1756: 1753: 1750: 1747: 1744: 1741: 1738: 1735: 1732: 1729: 1726: 1723: 1720: 1717: 1714: 1711: 1708: 1705: 1702: 1699: 1696: 1693: 1690: 1689:"Mike" 1687: 1684: 1681: 1678: 1675: 1672: 1669: 1666: 1663: 1660: 1657: 1654: 1651: 1648: 1645: 1642: 1639: 1638:"Mike" 1636: 1633: 1630: 1627: 1624: 1621: 1618: 1615: 1612: 1609: 1606: 1603: 1600: 1593: 1584:diamond operator 1581: 1570:Diamond operator 1565: 1562: 1559: 1556: 1553: 1550: 1547: 1544: 1541: 1538: 1535: 1532: 1529: 1526: 1523: 1520: 1517: 1507: 1504: 1501: 1498: 1495: 1492: 1489: 1486: 1483: 1480: 1477: 1474: 1471: 1468: 1465: 1462: 1452: 1449: 1446: 1443: 1440: 1437: 1434: 1431: 1428: 1425: 1407: 1404: 1401: 1398: 1395: 1392: 1389: 1386: 1383: 1380: 1377: 1374: 1371: 1368: 1365: 1362: 1359: 1356: 1346: 1343: 1340: 1337: 1334: 1331: 1328: 1325: 1322: 1319: 1316: 1313: 1310: 1307: 1304: 1294: 1287: 1284: 1281: 1278: 1275: 1272: 1269: 1266: 1263: 1260: 1257: 1254: 1251: 1248: 1245: 1242: 1239: 1236: 1233: 1230: 1227: 1224: 1221: 1218: 1215: 1212: 1209: 1206: 1203: 1200: 1197: 1176: 1173: 1170: 1167: 1164: 1161: 1158: 1155: 1152: 1149: 1146: 1143: 1140: 1137: 1134: 1131: 1128: 1125: 1122: 1119: 1116: 1113: 1110: 1107: 1104: 1101: 1098: 1095: 1092: 1089: 1086: 1083: 1080: 1077: 1074: 1071: 1068: 1065: 1062: 1059: 1056: 1053: 1050: 1047: 1044: 1041: 1038: 1035: 1032: 1029: 1026: 1023: 1020: 1017: 1014: 1011: 1008: 1005: 1002: 999: 996: 993: 990: 987: 984: 981: 978: 975: 972: 969: 966: 963: 960: 957: 954: 951: 948: 945: 942: 939: 936: 933: 932:"Mike" 930: 927: 924: 921: 918: 915: 912: 909: 906: 903: 900: 897: 894: 891: 888: 885: 882: 879: 876: 873: 870: 869:"Mike" 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 755: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 458: 450: 442: 432: 428: 424: 413: 409: 405: 401: 397: 393: 389: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 336:"test" 334: 331: 328: 325: 322: 319: 316: 313: 310: 307: 304: 301: 298: 295: 292: 289: 286: 276: 269: 266: 263: 260: 257: 254: 251: 248: 245: 242: 239: 236: 233: 230: 227: 224: 221: 220:"test" 218: 215: 212: 209: 206: 203: 200: 197: 194: 191: 188: 181: 177: 173: 169: 164: 156: 21: 3580: 3579: 3575: 3574: 3573: 3571: 3570: 3569: 3550: 3549: 3543: 3530: 3527: 3522: 3521: 3511: 3509: 3499: 3498: 3494: 3484: 3482: 3477: 3476: 3472: 3463: 3461: 3456: 3455: 3451: 3431: 3430: 3426: 3418: 3407: 3395: 3390: 3389: 3385: 3376: 3375: 3371: 3359: 3354: 3353: 3349: 3341: 3337: 3329: 3310: 3302: 3269: 3261: 3257: 3250: 3246: 3238: 3234: 3226: 3222: 3220: 3216: 3209: 3205: 3200: 3182:Wildcard (Java) 3168: 3143: 3108: 3102: 3037: 3031: 3011: 2996: 2995: 2992: 2989: 2986: 2983: 2980: 2977: 2974: 2971: 2968: 2965: 2962: 2959: 2956: 2953: 2950: 2947: 2944: 2941: 2938: 2922: 2921: 2918: 2915: 2912: 2909: 2906: 2903: 2900: 2897: 2894: 2891: 2888: 2885: 2882: 2879: 2876: 2873: 2870: 2867: 2864: 2861: 2858: 2855: 2852: 2849: 2846: 2843: 2840: 2837: 2834: 2831: 2828: 2825: 2822: 2819: 2816: 2813: 2810: 2807: 2804: 2801: 2798: 2795: 2792: 2789: 2786: 2783: 2777: 2776: 2773: 2770: 2767: 2764: 2761: 2758: 2755: 2752: 2745: 2742: 2741: 2738: 2735: 2732: 2729: 2726: 2723: 2720: 2717: 2714: 2711: 2708: 2705: 2702: 2699: 2696: 2693: 2690: 2687: 2684: 2681: 2678: 2675: 2672: 2669: 2666: 2663: 2660: 2657: 2654: 2651: 2648: 2645: 2642: 2639: 2636: 2633: 2630: 2627: 2624: 2621: 2618: 2615: 2612: 2609: 2599: 2595: 2591: 2587: 2583: 2579: 2575: 2571: 2567: 2560: 2556: 2555:. For example, 2549: 2544: 2543: 2540: 2537: 2534: 2531: 2528: 2525: 2522: 2519: 2516: 2513: 2510: 2507: 2504: 2501: 2498: 2495: 2492: 2489: 2486: 2483: 2480: 2477: 2474: 2471: 2468: 2465: 2462: 2459: 2453: 2421: 2417: 2413: 2408: 2405: 2402: 2399: 2396: 2391: 2387: 2382: 2379: 2376: 2373: 2370: 2365: 2362: 2361: 2358: 2355: 2352: 2349: 2346: 2343: 2340: 2337: 2334: 2331: 2328: 2325: 2322: 2319: 2316: 2313: 2310: 2307: 2304: 2301: 2298: 2295: 2292: 2289: 2286: 2280: 2279: 2276: 2273: 2270: 2267: 2264: 2261: 2258: 2255: 2252: 2249: 2246: 2243: 2240: 2237: 2234: 2231: 2228: 2225: 2222: 2219: 2216: 2213: 2210: 2207: 2204: 2201: 2198: 2195: 2192: 2189: 2186: 2183: 2180: 2177: 2174: 2171: 2168: 2165: 2162: 2159: 2156: 2153: 2150: 2147: 2140: 2136: 2132: 2131:that is not an 2128: 2124: 2120: 2116: 2112: 2108: 2104: 2097: 2093: 2089: 2085: 2080: 2077: 2074: 2071: 2068: 2062: 2056:To specify the 2046: 2042: 2038: 2034: 2030: 2026: 2023: 2022: 2019: 2016: 2013: 2010: 2007: 2004: 2001: 1998: 1995: 1992: 1989: 1986: 1983: 1980: 1977: 1974: 1971: 1968: 1965: 1962: 1959: 1956: 1953: 1950: 1947: 1944: 1937: 1926: 1922: 1920:Wildcard (Java) 1916: 1911: 1910: 1907: 1904: 1901: 1898: 1895: 1892: 1889: 1886: 1883: 1880: 1877: 1874: 1871: 1868: 1865: 1862: 1859: 1856: 1853: 1850: 1847: 1844: 1841: 1838: 1835: 1832: 1829: 1826: 1823: 1820: 1817: 1814: 1811: 1808: 1805: 1802: 1799: 1796: 1793: 1790: 1787: 1784: 1781: 1778: 1775: 1772: 1769: 1766: 1763: 1760: 1757: 1754: 1751: 1748: 1745: 1742: 1739: 1736: 1733: 1730: 1727: 1724: 1721: 1718: 1715: 1712: 1709: 1706: 1703: 1700: 1697: 1694: 1691: 1688: 1685: 1682: 1679: 1676: 1673: 1670: 1667: 1664: 1661: 1658: 1655: 1652: 1649: 1646: 1643: 1640: 1637: 1634: 1631: 1628: 1625: 1622: 1619: 1616: 1613: 1610: 1607: 1604: 1601: 1598: 1591: 1579: 1572: 1567: 1566: 1563: 1560: 1557: 1554: 1551: 1548: 1545: 1542: 1539: 1536: 1533: 1530: 1527: 1524: 1521: 1518: 1515: 1509: 1508: 1505: 1502: 1499: 1496: 1493: 1490: 1487: 1484: 1481: 1478: 1475: 1472: 1469: 1466: 1463: 1460: 1454: 1453: 1450: 1447: 1444: 1441: 1438: 1435: 1432: 1429: 1426: 1423: 1413:primitive types 1409: 1408: 1405: 1402: 1399: 1396: 1393: 1390: 1387: 1384: 1381: 1378: 1375: 1372: 1369: 1366: 1363: 1360: 1357: 1354: 1348: 1347: 1344: 1341: 1338: 1335: 1332: 1329: 1326: 1323: 1320: 1317: 1314: 1311: 1308: 1305: 1302: 1292: 1289: 1288: 1285: 1282: 1279: 1276: 1273: 1270: 1267: 1264: 1261: 1258: 1255: 1252: 1249: 1246: 1243: 1240: 1237: 1234: 1231: 1228: 1225: 1222: 1219: 1216: 1213: 1210: 1207: 1204: 1201: 1198: 1195: 1189: 1184: 1178: 1177: 1174: 1171: 1168: 1165: 1162: 1159: 1156: 1153: 1150: 1147: 1144: 1141: 1138: 1135: 1132: 1129: 1126: 1123: 1120: 1117: 1114: 1111: 1108: 1105: 1102: 1099: 1096: 1093: 1090: 1087: 1084: 1081: 1078: 1075: 1072: 1069: 1066: 1063: 1060: 1057: 1054: 1051: 1048: 1045: 1042: 1039: 1036: 1033: 1030: 1027: 1024: 1021: 1018: 1015: 1012: 1009: 1006: 1003: 1000: 997: 994: 991: 988: 985: 982: 979: 976: 973: 970: 967: 964: 961: 958: 955: 952: 949: 946: 943: 940: 937: 934: 931: 928: 925: 922: 919: 916: 913: 910: 907: 904: 901: 898: 895: 892: 889: 886: 883: 880: 877: 874: 871: 868: 865: 862: 859: 856: 853: 850: 847: 844: 841: 838: 835: 832: 829: 826: 823: 820: 817: 811: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 708: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 566: 561: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 454: 446: 438: 430: 426: 422: 411: 407: 403: 399: 395: 391: 387: 384: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 287: 284: 274: 271: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 219: 216: 213: 210: 207: 204: 201: 198: 195: 192: 189: 186: 179: 175: 171: 167: 160: 152: 148: 91: 28: 23: 22: 15: 12: 11: 5: 3578: 3576: 3568: 3567: 3562: 3552: 3551: 3548: 3547: 3542:978-0134685991 3541: 3526: 3523: 3520: 3519: 3500:Goetz, Brian. 3492: 3470: 3449: 3424: 3405: 3400:www.oracle.com 3383: 3369: 3364:www.oracle.com 3347: 3335: 3308: 3267: 3255: 3244: 3232: 3214: 3202: 3201: 3199: 3196: 3195: 3194: 3189: 3184: 3179: 3174: 3167: 3164: 3163: 3162: 3156: 3142: 3139: 3104:Main article: 3101: 3098: 3033:Main article: 3030: 3027: 3010: 3007: 2937: 2782: 2751: 2608: 2548: 2545: 2458: 2452: 2449: 2445:contravariance 2433:Effective Java 2285: 2146: 1943: 1918:Main article: 1915: 1914:Type wildcards 1912: 1597: 1576:type inference 1571: 1568: 1514: 1459: 1422: 1353: 1301: 1194: 1188: 1185: 1182: 816: 787:", " 575: 565: 562: 463: 440:java.util.List 410:is defined as 283: 185: 147: 144: 143: 142: 134: 126: 116: 108: 90: 87: 75:Martin Odersky 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 3577: 3566: 3563: 3561: 3558: 3557: 3555: 3544: 3538: 3534: 3529: 3528: 3524: 3507: 3503: 3496: 3493: 3480: 3474: 3471: 3460: 3453: 3450: 3446: 3442: 3438: 3434: 3433:Bracha, Gilad 3428: 3425: 3421: 3416: 3414: 3412: 3410: 3406: 3401: 3394: 3387: 3384: 3379: 3373: 3370: 3365: 3358: 3351: 3348: 3344: 3339: 3336: 3332: 3327: 3325: 3323: 3321: 3319: 3317: 3315: 3313: 3309: 3305: 3300: 3298: 3296: 3294: 3292: 3290: 3288: 3286: 3284: 3282: 3280: 3278: 3276: 3274: 3272: 3268: 3264: 3259: 3256: 3253: 3248: 3245: 3241: 3236: 3233: 3225: 3218: 3215: 3212: 3207: 3204: 3197: 3193: 3190: 3188: 3185: 3183: 3180: 3178: 3175: 3173: 3170: 3169: 3165: 3160: 3157: 3154: 3151: 3150: 3149: 3147: 3140: 3138: 3127:List<T> 3123: 3119: 3115: 3113: 3107: 3099: 3097: 3096: 3067: 3044: 3042: 3036: 3028: 3026: 3024: 3020: 3008: 3006: 3003: 3001: 2935: 2932: 2930: 2929:C++ templates 2925: 2780: 2749: 2606: 2603: 2564: 2554: 2546: 2456: 2450: 2448: 2446: 2442: 2438: 2434: 2429: 2427: 2283: 2144: 2101: 2059: 2054: 2052: 1941: 1934: 1932: 1921: 1913: 1644:"A" 1595: 1589: 1585: 1582:, called the 1577: 1569: 1512: 1457: 1420: 1418: 1414: 1351: 1299: 1296: 1192: 1186: 1181: 875:"A" 814: 799:")" 775:"(" 573: 571: 563: 461: 459: 457: 451: 449: 443: 441: 434: 420: 415: 281: 278: 183: 165: 163: 157: 155: 145: 139: 135: 131: 127: 123: 122: 117: 113: 109: 106: 105: 104:type variable 100: 99: 98: 96: 93:According to 88: 86: 84: 80: 79:Philip Wadler 76: 72: 67: 65: 60: 58: 54: 53: 48: 44: 40: 36: 32: 19: 18:Java generics 3532: 3510:. Retrieved 3505: 3495: 3483:. Retrieved 3473: 3462:. Retrieved 3452: 3444: 3440: 3427: 3402:. p. 5. 3399: 3386: 3372: 3363: 3350: 3338: 3258: 3247: 3235: 3217: 3206: 3144: 3124: 3120: 3116: 3109: 3068: 3045: 3038: 3012: 3004: 2997: 2933: 2926: 2923: 2778: 2743: 2604: 2582:contains an 2565: 2553:type erasure 2550: 2454: 2437:Joshua Bloch 2432: 2430: 2363: 2281: 2102: 2055: 2024: 1935: 1923: 1583: 1573: 1510: 1455: 1410: 1349: 1297: 1293:<Type> 1290: 1190: 1180:It outputs: 1179: 812: 567: 453: 445: 437: 435: 419:compile-time 416: 385: 279: 272: 159: 151: 149: 137: 129: 119: 111: 102: 94: 92: 71:Gilad Bracha 68: 61: 50: 30: 29: 3110:Arrays are 3100:Reification 3023:reification 2520:conditional 2490:conditional 2447:) in Java. 2058:upper bound 1411:The use of 452:in package 429:instead of 402:'s generic 138:constructor 115:at runtime. 47:type system 3554:Categories 3525:References 3485:24 October 3464:2010-04-20 3443:. Oracle. 3420:Bloch 2018 3343:Bloch 2018 3331:Bloch 2018 3304:Bloch 2018 3240:Bloch 2018 2594:, such as 2441:covariance 2359:// allowed 2020:// allowed 1948:Collection 1574:Thanks to 146:Motivation 133:interface. 3512:12 August 3508:. OpenJDK 3198:Citations 3041:covariant 2774:Exception 2746:Throwable 2667:ArrayList 2634:ArrayList 2580:ArrayList 2568:ArrayList 2532:exception 2499:exception 2472:Throwable 2172:ArrayList 1963:ArrayList 1951:<?> 1927:<?> 733:ValueType 652:ValueType 625:ValueType 595:ValueType 522:interface 465:interface 456:java.util 400:ArrayList 392:ArrayList 309:ArrayList 202:ArrayList 172:ArrayList 154:ArrayList 121:interface 83:wildcards 69:In 1998, 3481:. Oracle 3166:See also 3133:, where 3058:to this 3019:variance 2703:getClass 2688:getClass 2670:<> 2637:<> 2175:<> 1824:getValue 1794:<> 1683:<> 1632:<> 1580:<> 1500:elements 1488:elements 1079:getValue 763:toString 736:getValue 525:Iterator 513:iterator 501:Iterator 425:returns 423:v.get(0) 408:v.get(0) 158:of type 125:runtime. 31:Generics 3112:reified 2907:println 2853:println 2826:Integer 2802:Integer 2771:extends 2727:println 2619:Integer 2592:Integer 2588:Integer 2584:Integer 2487:boolean 2469:extends 2296:extends 2250:Integer 2157:Integer 2133:Integer 2113:Integer 2075:extends 2063:extends 1884:println 1842:println 1776:Boolean 1770:Integer 1743:println 1713:println 1665:Integer 1588:implies 1525:toArray 1516:Integer 1476:toArray 1148:println 1100:println 1046:Boolean 1040:Integer 1019:Boolean 1013:Integer 986:println 956:println 923:Integer 896:Integer 706:KeyType 643:KeyType 619:private 610:KeyType 604:private 589:KeyType 552:hasNext 549:boolean 431:Integer 357:Integer 345:Integer 244:Integer 232:Integer 180:Integer 170:to the 3539:  3081:using 3056:String 3048:Object 3015:Object 2978:return 2895:System 2880:String 2841:System 2753:public 2715:System 2600:Object 2596:Number 2574:or an 2505:throws 2460:public 2422:Number 2418:Number 2414:Object 2406:Number 2380:Number 2299:Number 2208:Number 2129:Number 2117:Number 2098:Number 2086:Number 2078:Number 1993:Object 1969:String 1896:getKey 1872:System 1854:getKey 1830:System 1731:System 1701:System 1659:String 1614:String 1608:String 1497:return 1461:public 1391:String 1370:String 1364:String 1318:String 1312:String 1247:return 1199:static 1196:public 1160:getKey 1136:System 1112:getKey 1088:System 974:System 944:System 917:String 890:String 860:String 854:String 833:String 827:String 772:return 760:String 757:public 745:return 730:public 718:return 709:getKey 703:public 634:public 577:public 427:String 412:String 404:Object 396:String 388:String 315:String 294:String 176:String 168:String 162:Object 130:method 3396:(PDF) 3360:(PDF) 3227:(PDF) 2868:catch 2814:catch 2790:throw 2756:class 2652:Float 2643:final 2610:final 2529:throw 2403:super 2400:<? 2377:super 2374:<? 2366:super 2317:// OK 2293:<? 2287:final 2247:final 2199:final 2148:final 2072:<? 2047:add() 2031:add() 1945:final 1931:bound 1890:prime 1848:prime 1818:prime 1791:Entry 1782:prime 1764:Entry 1761:final 1725:grade 1680:Entry 1653:Entry 1650:final 1629:Entry 1620:grade 1602:Entry 1599:final 1592:Entry 1519:array 1427:Entry 1424:final 1417:boxed 1397:twice 1382:Entry 1358:Entry 1355:final 1336:twice 1330:Entry 1306:Entry 1303:final 1280:value 1274:value 1253:Entry 1238:value 1229:twice 1211:Entry 1154:prime 1106:prime 1073:prime 1034:Entry 1025:prime 1007:Entry 1004:final 968:grade 911:Entry 884:Entry 881:final 848:Entry 839:grade 821:Entry 818:final 793:value 748:value 694:value 688:value 655:value 637:Entry 628:value 622:final 607:final 583:Entry 580:class 342:final 285:final 229:final 187:final 112:class 3537:ISBN 3514:2014 3487:2015 3129:and 3060:Long 3052:Long 3021:and 2966:> 2960:< 2957:List 2945:> 2939:< 2883:> 2877:< 2829:> 2823:< 2805:> 2799:< 2768:> 2762:< 2655:> 2649:< 2646:List 2622:> 2616:< 2613:List 2561:List 2478:void 2475:> 2463:< 2443:and 2426:null 2409:> 2397:List 2383:> 2371:List 2353:null 2341:nums 2332:3.14 2320:nums 2311:ints 2305:nums 2302:> 2290:List 2259:ints 2241:3.14 2229:nums 2220:ints 2214:nums 2211:> 2205:< 2202:List 2181:ints 2163:ints 2160:> 2154:< 2151:List 2107:nor 2081:> 2069:List 2051:null 2014:null 1996:()); 1972:> 1966:< 1869:else 1806:true 1779:> 1767:< 1755:mark 1671:mark 1668:> 1656:< 1617:> 1605:< 1482:Type 1473:Type 1470:> 1467:Type 1464:< 1445:pair 1442:> 1430:< 1394:> 1388:< 1376:pair 1373:> 1361:< 1324:pair 1321:> 1309:< 1268:> 1265:Type 1259:Type 1256:< 1235:Type 1226:> 1223:Type 1217:Type 1214:< 1208:> 1205:Type 1202:< 1130:else 1061:true 1049:> 1037:< 1022:> 1010:< 998:mark 926:> 914:< 902:mark 899:> 887:< 863:> 851:< 836:> 824:< 682:this 664:this 598:> 586:< 543:next 534:> 528:< 510:> 504:< 483:void 477:> 471:< 468:List 444:and 318:> 312:< 297:> 291:< 288:List 190:List 62:The 43:J2SE 2987:(); 2981:new 2969:arg 2901:err 2847:err 2808:(); 2793:new 2784:try 2721:out 2706:()) 2673:(); 2664:new 2640:(); 2631:new 2602:). 2598:or 2435:by 2424:or 2390:or 2347:add 2326:add 2265:get 2235:add 2187:add 2178:(); 2169:new 2092:or 2008:add 1990:new 1984:add 1975:(); 1960:new 1878:out 1836:out 1827:()) 1788:new 1737:out 1707:out 1695:100 1677:new 1626:new 1485:... 1439:int 1433:int 1250:new 1142:out 1094:out 1082:()) 1031:new 980:out 950:out 938:100 908:new 845:new 781:key 721:key 676:key 670:key 646:key 613:key 570:map 555:(); 546:(); 516:(); 486:add 369:get 330:add 321:(); 306:new 256:get 214:add 205:(); 199:new 118:An 3556:: 3504:. 3439:. 3435:. 3408:^ 3398:. 3362:. 3311:^ 3270:^ 3025:. 2916:); 2862:); 2736:); 2697:lf 2694:== 2691:() 2682:li 2676:if 2658:lf 2625:li 2514:if 2356:); 2335:); 2274:); 2244:); 2196:); 2143:: 2017:); 1908:); 1899:() 1866:); 1857:() 1812:if 1809:); 1800:13 1758:); 1728:); 1698:); 1647:); 1564:); 1406:); 1345:); 1283:); 1172:); 1163:() 1124:); 1115:() 1067:if 1064:); 1055:13 1001:); 971:); 941:); 878:); 766:() 739:() 712:() 572:: 498:); 460:: 378:); 339:); 265:); 223:); 136:A 128:A 110:A 101:A 97:: 85:. 73:, 3545:. 3516:. 3489:. 3467:. 3380:. 3366:. 3229:. 3135:T 2993:} 2984:T 2975:{ 2972:) 2963:T 2954:( 2948:T 2942:T 2919:} 2910:( 2904:. 2898:. 2892:{ 2889:) 2886:e 2871:( 2865:} 2856:( 2850:. 2844:. 2838:{ 2835:) 2832:e 2817:( 2811:} 2787:{ 2765:T 2739:} 2730:( 2724:. 2718:. 2709:{ 2700:. 2685:. 2679:( 2661:= 2628:= 2541:} 2538:} 2535:; 2526:{ 2523:) 2517:( 2511:{ 2508:T 2502:) 2496:T 2493:, 2484:( 2466:T 2350:( 2344:. 2329:( 2323:. 2314:; 2308:= 2271:1 2268:( 2262:. 2256:= 2253:x 2238:( 2232:. 2223:; 2217:= 2193:2 2190:( 2184:. 2166:= 2043:? 2035:E 2027:c 2011:( 2005:. 2002:c 1987:( 1981:. 1978:c 1957:= 1954:c 1925:" 1902:+ 1893:. 1887:( 1881:. 1875:. 1860:+ 1851:. 1845:( 1839:. 1833:. 1821:. 1815:( 1803:, 1797:( 1785:= 1773:, 1752:+ 1746:( 1740:. 1734:. 1722:+ 1716:( 1710:. 1704:. 1692:, 1686:( 1674:= 1662:, 1641:, 1635:( 1623:= 1611:, 1561:6 1558:, 1555:5 1552:, 1549:4 1546:, 1543:3 1540:, 1537:2 1534:, 1531:1 1528:( 1522:= 1506:} 1503:; 1494:{ 1491:) 1479:( 1448:; 1436:, 1400:( 1385:. 1379:= 1367:, 1339:( 1333:. 1327:= 1315:, 1286:} 1277:, 1271:( 1262:, 1244:{ 1241:) 1232:( 1220:, 1175:} 1166:+ 1157:. 1151:( 1145:. 1139:. 1133:{ 1127:} 1118:+ 1109:. 1103:( 1097:. 1091:. 1085:{ 1076:. 1070:( 1058:, 1052:( 1043:, 1028:= 1016:, 995:+ 989:( 983:. 977:. 965:+ 959:( 953:. 947:. 935:, 929:( 920:, 905:= 893:, 872:, 866:( 857:, 842:= 830:, 808:} 805:} 802:; 796:+ 790:+ 784:+ 778:+ 769:{ 754:} 751:; 742:{ 727:} 724:; 715:{ 700:} 697:; 691:= 685:. 679:; 673:= 667:. 661:{ 658:) 649:, 640:( 631:; 616:; 601:{ 592:, 558:} 540:E 537:{ 531:E 519:} 507:E 495:x 492:E 489:( 480:{ 474:E 375:0 372:( 366:. 363:v 360:) 354:( 351:= 348:i 333:( 327:. 324:v 303:= 300:v 262:0 259:( 253:. 250:v 247:) 241:( 238:= 235:i 217:( 211:. 208:v 196:= 193:v 20:)

Index

Java generics
generic programming
Java programming language
J2SE
type system
compile-time type safety
Java virtual machine
Java collections framework
Gilad Bracha
Martin Odersky
Philip Wadler
wildcards
type variable
interface
ArrayList
Object
compile-time
java.util.List
java.util.Iterator
java.util
map
primitive types
boxed
type inference
implies
Wildcard (Java)
bound
null
upper bound
null

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.