Wildcards on Java

Share Tutorial Java Programing – Wildcards on Java

Share Tutorial Java - Wildcards

Consider the problem of writing a routine that prints out all the elements in a collection.
Here’s how you might write it in an older version of the language:

void printCollection(Collection c) {
Iterator i = c.iterator();
for (k = 0; k < c.size(); k++) {
System.out.println(i.next());
}}

And here is a naive attempt at writing it using generics (and the new for loop syntax):

void printCollection(Collection<Object> c) {
for (Object e : c) {
System.out.println(e);
}}

The problem is that this new version is much less useful than the old one. Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which, as we’ve just demonstrated, is not a supertype of all kinds of collections!

So what is the supertype of all kinds of collections? It’s written Collection<?> (pronounced “collection of unknown”) , that is, a collection whose element typematches anything. It’s called a wildcard type for obvious reasons. We can write:

void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}}

and now, we can call it with any type of collection. Notice that inside printCollection(), we can still read elements from c and give them type Object. This is always safe, since whatever the actual type of the collection, it does contain objects. It isn’t safe to add arbitrary objects to it however:

Collection<?> c = new ArrayList<String>();
c.add(new Object()); // compile time error

Since we don’t know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection.

When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add 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 null, which is a member of every type.

On the other hand, given a List<?>, we can call get() and make use of the result.

The result type is an unknown type, but we always know that it is an object. It is therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected.

Share Tutorial Java Programing – Wildcards on Java

Acquire the most preferred wordpress plugin post readily available today, you'll find both free and premium plugins right here. We have inspected the whole internet, assessed hundreds of recommended Wordpress plugins and decided on the best.
Locate technology news the most recent modern technology headlines, consisting of new item launches, revenues figures and tech market efficiency info. Review posts on brand-new gadgets and prototypes for future technology.
small business seo people need to and need to not be doing on your Websites to make them place greater in search engines. In a regularly altering Search Engine Optimization garden, it is essential to continue to be current with the transforming practices and strategies of optimization.
Below is a list of system blog network management from a main place. There are WordPress dash panels, along with multisite tools to handle domains, styles, initiatives, and more. consists of one-click access, monitoring, back-up, implementation, posting, and protection attributes. Evaluation which of your sites have motifs and plugins that need attention. With one click, upgrade all of your plugins, styles or center WordPress software.


My Name is Adi Saputra. I come from Indonesia. I want to develop www.tuto-rial.com Blog Being the best, So much of it as a reference in the world of IT in particular programming

Comments are closed.