Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:37:48+00:00 2026-05-16T07:37:48+00:00

Recently, I read this article: http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html My question is, instead of creating a method

  • 0

Recently, I read this article:
http://download.oracle.com/javase/tutorial/extra/generics/wildcards.html

My question is, instead of creating a method like this:

public void drawAll(List<? extends Shape> shapes){
    for (Shape s: shapes) {
        s.draw(this);
    }
}

I can create a method like this, and it works fine:

public <T extends Shape> void drawAll(List<T> shapes){
    for (Shape s: shapes) {
        s.draw(this);
    }
}

Which way should I use? Is wildcard useful in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:37:49+00:00Added an answer on May 16, 2026 at 7:37 am

    It depends on what you need to do. You need to use the bounded type parameter if you wanted to do something like this:

    public <T extends Shape> void addIfPretty(List<T> shapes, T shape) {
        if (shape.isPretty()) {
           shapes.add(shape);
        }
    }
    

    Here we have a List<T> shapes and a T shape, therefore we can safely shapes.add(shape). If it was declared List<? extends Shape>, you can NOT safely add to it (because you may have a List<Square> and a Circle).

    So by giving a name to a bounded type parameter, we have the option to use it elsewhere in our generic method. This information is not always required, of course, so if you don’t need to know that much about the type (e.g. your drawAll), then just wildcard is sufficient.

    Even if you’re not referring to the bounded type parameter again, a bounded type parameter is still required if you have multiple bounds. Here’s a quote from Angelika Langer’s Java Generics FAQs

    What is the difference between a wildcard bound and a type parameter bound?

    A wildcard can have only one bound, while a type parameter can have several bounds.
    A wildcard can have a lower or an upper bound, while there is no such thing as a lower bound for a type parameter.

    Wildcard bounds and type parameter bounds are often confused, because they are both called bounds and have in part similar syntax. […]

    Syntax:

      type parameter bound     T extends Class & Interface1 & … & InterfaceN
    
      wildcard bound  
          upper bound          ? extends SuperType
          lower bound          ? super   SubType
    

    A wildcard can have only one bound, either a lower or an upper bound. A list of wildcard bounds is not permitted.

    A type parameter, in constrast, can have several bounds, but there is no such thing as a lower bound for a type parameter.

    Quotes from Effective Java 2nd Edition, Item 28: Use bounded wildcards to increase API flexibility:

    For maximum flexibility, use wildcard types on input parameters that represent producers or consumers. […] PECS stands for producer-extends, consumer-super […]

    Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code. Properly used, wildcard types are nearly invisible to users of a class. They cause methods to accept the parameters they should accept and reject those they should reject. If the user of the class has to think about wildcard types, there is probably something wrong with the class’s API.

    Applying the PECS principle, we can now go back to our addIfPretty example and make it more flexible by writing the following:

    public <T extends Shape> void addIfPretty(List<? super T> list, T shape) { … }
    

    Now we can addIfPretty, say, a Circle, to a List<Object>. This is obviously typesafe, and yet our original declaration was not flexible enough to allow it.

    Related questions

    • Java Generics: What is PECS?
    • Can someone explain what does <? super T> mean and when should it be used and how this construction should cooperate with <T> and <? extends T>?

    Summary

    • Do use bounded type parameters/wildcards, they increase flexibility of your API
    • If the type requires several parameters, you have no choice but to use bounded type parameter
    • if the type requires a lowerbound, you have no choice but to use bounded wildcard
    • “Producers” have upperbounds, “consumers” have lowerbounds
    • Do not use wildcard in return types
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read an interesting article recently on the CitiGroup Hacking incident http://www.nytimes.com/2011/06/14/technology/14security.html?_r=2&pagewanted=1&ref=technology This got
Recently I've read this article: http://www.smashingmagazine.com/2009/09/25/svn-strikes-back-a-serious-vulnerability-found/ Developers of many popular sites like apache.org, php.net
I've recently read an article http://www.ravelrumba.com/blog/static-cookieless-domain/ about Serving Static Content from a Cookieless Domain
I have recently read the safe bool idiom article. I had seen this technique
I recently read an article talking about the Java annotations, and on this latter
I recently read this article . Which uses the Silverlight Bing maps control to
I recently read this article Safe Thread Synchronization as I was curious about the
I recently read this article on smart use of ViewState and am particularly interested
I've recently read this article on using printf and scanf in assembly: Meaning of
Recently I read this Wikipedia article regarding the Dining Philosophers problem but I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.