<pedrocorreia.net ⁄>

<Java theory and practice: Going wild with generics ⁄ >

clicks: 2596 2596 2008-05-07 2008-05-07 goto programacao myNews programacao  Bookmark This Bookmark This

Generics are a means of expressing type constraints on the behavior of a class or method in terms of unknown types, such as "whatever the types of parameters x and y of this method are, they must be the same type," "you must provide a parameter of the same type to both of these methods," or "the return value of foo() is the same type as the parameter of bar()."

Wildcards - the funky question marks where a type parameter should go - are a means of expressing a type constraint in terms of an unknown type. They were not part of the original design for generics (derived from the Generic Java (GJ) project); they were added as the design process played out over the five years between the formation of JSR 14 and its final release.

Wildcards play an important role in the type system; they provide a useful type bound for the family of types specified by a generic class. For the generic class ArrayList, the type ArrayList is a supertype of ArrayList for any (reference) type T (as are the raw type ArrayList and the root type Object, but these supertypes are far less useful for performing type inference).

The wildcard type List is different from both the raw type List and the concrete type List. To say a variable x has type List means that there exists some type T for which x is of type List, that x is homogeneous even though we don't know what particular type its elements have. It's not that the contents can be anything, it's that we don't know what the type constraints on the contents are - but we know that there is a constraint. On the other hand, the raw type List is heterogeneous; we are not able to place any type constraints on its elements, and the concrete type List means that we explicitly know that it can contain any object. (Of course, the generic type system has no concept of "the contents of a list," but it is easiest to understand generics in terms of collection types like List.)

The utility of wildcards in the type system comes partially from the fact that generic types are not covariant. Arrays are covariant; because Integer is a subtype of Number, the array type Integer[] is a subtype of Number[], and therefore an Integer[] value can be supplied wherever a value of Number[] is required. On the other hand, generics are not covariant; List is not a subtype of List, and attempting to supply a List where a List is demanded is a type error. This was not an accident - nor necessarily the error that everyone assumes it to be - but the different behavior of generics vs. arrays does cause a great deal of confusion.

este é só um excerto do artigo, para aceder ao artigo completo, clique no link em baixo:
this is just a small excerpt from the article, to access the full article please click in the link below:


Subscribe News RSS  Subscribe News Updates by E-mail

myNews <myNews show="rand" cat="programacao" ⁄>

RouterJs: easy routing for your ajax Web applications new ...

RouterJs is a simple router for your ajax web apps. It's build upon History.js which means that Rout (...)

clicks: 15480 15480 2012-05-14 2012-05-14 goto url (new window) haithembelhaj.g... goto myNews programacao

Backbone computed properties new ...

This gist shows one way to implement read- and write-enabled computed properties on a Backbone Model (...)

clicks: 15261 15261 2012-05-13 2012-05-13 goto url (new window) https://gist.gi... goto myNews programacao

Create Instagram Filters With PHP new ...

In this tutorial, I'll demonstrate how to create vintage (just like Instagram does) photos with PHP (...)

clicks: 14943 14943 2012-05-12 2012-05-12 goto url (new window) net.tutsplus.co... goto myNews programacao

Android Query new ...

Android-Query (AQuery) is a light-weight library for doing asynchronous tasks and manipulating UI el (...)

clicks: 15494 15494 2012-05-12 2012-05-12 goto url (new window) code.google.com... goto myNews programacao

HTML5 jQuery Paint Plugin new ...

Websanova Paint is a HTML5 canvas based jQuery plugin. It allows you to free paint on a canvas area (...)

clicks: 25971 25971 2012-05-12 2012-05-12 goto url (new window) websanova.com/t... goto myNews programacao

Real-time Applications With Node.js and Socket.IO new ...

Hey everyone! Sorry about the long pause since the last blog post, life has been quite hectic for th (...)

clicks: 15379 15379 2012-05-11 2012-05-11 goto url (new window) codingcookies.c... goto myNews programacao

Sass vs. LESS vs. Stylus: Preprocessor Shootout new ...

CSS3 preprocessors are languages written for the sole purpose of adding cool, inventive features to (...)

clicks: 14710 14710 2012-05-11 2012-05-11 goto url (new window) net.tutsplus.co... goto myNews programacao

15 Handpicked jQuery Drop Down Menus Tutorials new ...

Here we are presenting another brilliant collection of 15 jQuery navigation menu that you can downlo (...)

clicks: 14514 14514 2012-05-10 2012-05-10 goto url (new window) smashingapps.co... goto myNews programacao