0

As we konw, we can't add any element except null into an list like List<? extends Number>, so why the sentence can be compiled, wait for your answer, thanks!

List<? extends Number> arr = Arrays.asList(1,2,3.1f,4.1d)
wanglong
  • 89
  • 5

2 Answers2

3

That code is not "adding" an element to a List<? extends Number>. Adding an element into a List<? extends Number> looks something like:

arr.add(1);

which doesn't compile as you'd expect.

What your code is doing is creating a list containing 1, 2, 3.1, and 4.1, using Arrays.asList. Then you assign that list to the variable arr. You never added to arr.

When we say "you can only add null to a list of type List<? extends Number>", we really only mean that if you have a variable of type List<? extends Number> (arr in this case), you can only pass null to its add method (or any other method that accepts its generic type parameter).

It does not mean that there is no absolutely no way to add elements to the list object referenced by that variable. You just can't add to it by directly calling the add method.

For example,

ArrayList<Integer> ints = new ArrayList<>();
ArrayList<? extends Number> numbers = ints; // now numbers and ints refer to the same list
System.out.println(numbers); // prints []
ints.add(1);
System.out.println(numbers); // prints [1]
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • but the `? extends Number` does not refer to an absolutely type, why the list can contain element, and even different type element, so why, i can't understand – wanglong Mar 25 '21 at 07:03
  • `List extends Number>` is just the type of the variable. The actual list object is created by `Arrays.asList`. What specific kind of list that creates is their implementation detail. And of course `Arrays.asList` can create lists that has elements. That's what it's designed to do. You are right that `?` does not refer to an exact type, so what? The variable's type doesn't need to tell you _exactly_ the type of object the variable refers to. @wanglong – Sweeper Mar 25 '21 at 07:06
  • first thanks for your comment, then what you said that we don't need to know the variable type, i can't understand, can you explain clearly. @Sweeper – wanglong Mar 25 '21 at 07:13
  • @wanglong It's very hard to see what you understand and not understand. You seem to not understand the difference between objects and variables, so maybe read [this](https://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference) first. You also seem to fail to understand the idea of programming to interfaces, thinking that we need to know the exact type of every object, so read [this](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) as well. – Sweeper Mar 25 '21 at 07:20
  • @wanglong Once you read that, reread my answer and comment and hopefully you understand more. – Sweeper Mar 25 '21 at 07:26
  • i have understand what you said, but now i produce some new questions. @Sweeper – wanglong Mar 26 '21 at 07:29
0

It seems you are having an import issue here, regarding util.Arrays and util.List. There are functional errors in the code.

import java.util.Arrays;
import java.util.List;

public class Runner {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<? extends Number> arr = Arrays.asList(1,2,3.1f,4.1d);
        arr.forEach(e -> {
            System.out.println(e);
        });
    
    }

}

Output: enter image description here

utsab_c
  • 54
  • 6