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 8419955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:45:33+00:00 2026-06-10T02:45:33+00:00

Reading the Java online tutorial I haven’t understood anything about wildcard capture. For example:

  • 0

Reading the Java online tutorial I haven’t understood anything about wildcard capture.
For example:

    import java.util.List;
    public class WildcardError {
     void foo(List<?> i) {
      i.set(0, i.get(0));
     }
    }

Why can’t the compiler retain the assignment safely?

It knows that, by executing for instance, the method with an Integer List, it gets from i.get an Integer value. So it tries to set an Integer value at index 0 to the same Integer list (i).

So, what’s wrong? Why write Wildcard helper?

  • 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-06-10T02:45:35+00:00Added an answer on June 10, 2026 at 2:45 am

    why the compiler can’t retain the assignment safe?

    The compiler doesn’t know anything about the type of elements in List<?> i, by definition of ?. Wildcard does not mean “any type;” it means “some unknown type.”

    It knows that,by executing for instance, the method with an Integer List, it gets from i.get an Integer value.

    That’s true, but as I said above: the compiler can only know – at compile time, remember – that i.get(0) returns an Object, which is the upper bound of ?. But there’s no guarantee that ? is at runtime Object, so there is no way for the compiler to know that i.set(0, i.get(0)) is a safe call. It’s like writing this:

    List<Foo> fooz = /* init */;
    Object foo = fooz.get(0);
    fooz.set(0, foo); // won't compile because foo is an object, not a Foo
    

    More reading:

    • Can't add value to the Java collection with wildcard generic type
    • Java Collections using wildcard
    • Generic collection & wildcard in java
    • Generics – Cannot add to a List with unbounded wildcard
    • What is the difference betwen Collection<?> and Collection<T>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, so I was reading the Java online tutorial for generics and I found
I am reading the java tutorial about Wildcards in Generics. In the following code:
I'm learning Java by reading the online Java book, but im finding it hard
I am reading Java Concurrency in practice and looking at the example code on
I am reading Java Concurrency in practice and looking at the example code on
I've been reading about java/spring/hibernate and worked trough a dummy examples so I told
When reading articles about Java threads, I often notice the expression: current thread is
I'm currently reading a java concurrency tutorial in http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html I could not understand what
I've been reading about Java Transaction , and i've been confused about what's it?
I am reading some Java text about RandomAccessFile, in the text, the author says:

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.