Knowledge (XXG)

Generics in Java

Source 📝

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

Index

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
Joshua Bloch

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