Unbounded Wildcards

https://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html

The unbounded wildcard type is specified using the wildcard character (?), for example, List<?>. This is called a list of unknown type. There are two scenarios where an unbounded wildcard is a useful approach:

If you are writing a method that can be implemented using functionality provided in the Object class.
When the code is using methods in the generic class that don’t depend on the type parameter. For example, List.size or List.clear. In fact, Class<?> is so often used because most of the methods in Class do not depend on T.

Consider the following method, printList:
public static void printList(List list) {
for (Object elem : list)
System.out.println(elem + " ");
System.out.println();
}

The goal of printList is to print a list of any type, but it fails to achieve that goal — it prints only a list of Object instances; it cannot print List, List, List, and so on, because they are not subtypes of List. To write a generic printList method, use List<?>:
public static void printList(List<?> list) {
for (Object elem: list)
System.out.print(elem + " ");
System.out.println();
}

Because for any concrete type A, List is a subtype of List<?>, you can use printList to print a list of any type:
List li = Arrays.asList(1, 2, 3);
List ls = Arrays.asList(“one”, “two”, “three”);
printList(li);
printList(ls);

Note: The
Arrays.asList method is used in examples throughout this lesson. This static factory method converts the specified array and returns a fixed-size list.

It’s important to note that List and List<?> are not the same. You can insert an Object, or any subtype of Object, into a List. But you can only insert null into a List<?>. The
Guidelines for Wildcard Use section has more information on how to determine what kind of wildcard, if any, should be used in a given situation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值