和有什么不一样?和 在 Java 泛型中?

发布于 2024-12-14 21:47:12 字数 121 浏览 0 评论 0 原文

我之前见过通配符用来表示任何对象 - 但最近看到了这样的用法:

<? extends Object>

由于所有对象都扩展对象,这两种用法是同义的吗?

I've seen the wildcard used before to mean any object - but recently saw a use of:

<? extends Object>

Since all objects extend Object, are these two usages synonymous?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2024-12-21 21:47:13

是同义词。

在某些情况下,泛型的 extends Object 实际上并不是多余的。例如, 将导致 T 在擦除时变为 Object,而使用 时,它将变为 Foo 正在被删除。 (如果您试图保持与使用 Object 的预泛型 API 的兼容性,这可能很重要。)

来源:http://download.oracle.com/javase/tutorial/extra/generics/convert.html;它解释了为什么 JDK 的 java.util.Collections 类有一个具有以下签名的方法:

public static <T extends Object & Comparable<? super T>> T max(
    Collection<? extends T> coll
)

<?> and <? extends Object> are synonymous, as you'd expect.

There are a few cases with generics where extends Object is not actually redundant. For example, <T extends Object & Foo> will cause T to become Object under erasure, whereas with <T extends Foo> it will become Foo under erasure. (This can matter if you're trying to retain compatibility with a pre-generics API that used Object.)

Source: http://download.oracle.com/javase/tutorial/extra/generics/convert.html; it explains why the JDK's java.util.Collections class has a method with this signature:

public static <T extends Object & Comparable<? super T>> T max(
    Collection<? extends T> coll
)
南城旧梦 2024-12-21 21:47:13

尽管 应该是 的快捷方式扩展对象>,两者之间有微小的区别。
是可具体化的,而 不是。他们这样做的原因是为了更容易区分可具体化的类型。有任何看起来像 < 的东西吗? extends some> 是不可具体化的。

例如,此代码可以工作

List aList = new ArrayList<>();
boolean instanceTest = aList instanceof List<?>;

,但会出现错误

List aList = new ArrayList<>();
boolean instancetest = aList instanceof List<? extends Object>;

,请阅读 Maurice Naftalin 的 Java generics and collections

Although <?> is supposed to be a shortcut for <? extend object>, there is a tiny difference between the two.
<?> is reifiable while <? extend object> is not. The reason they did this is to make it easier to distinguish reifiable type. Anything that looks like <? extends something>,<T>,<Integer> are nonreifiable.

For example, this code would work

List aList = new ArrayList<>();
boolean instanceTest = aList instanceof List<?>;

but this gives an error

List aList = new ArrayList<>();
boolean instancetest = aList instanceof List<? extends Object>;

for more info read Java generics and collections by Maurice Naftalin

触ぅ动初心 2024-12-21 21:47:13


您可以阅读下面的共享链接以了解更多详细信息。


<?>

“?”表示任何未知类型,它可以代表代码中的任何类型。如果您不确定类型,请使用此通配符。

ArrayList<?> unknownList = new ArrayList<Number>(); //can accept of type Number
unknownList = new ArrayList<Float>(); //Float is of type Number

注意: 表示任何内容。所以它可以接受不是从Object类继承的类型。

<? extends Object>

<代码> 表示您可以传递一个 Object 或扩展 Object 类的子类。

ArrayList<? extends Number> numberList = new ArrayList<Number>(); //Number of subclass
numberList = new ArrayList<Integer>(); //Integer extends Number
numberList = new ArrayList<Float>(); // Float extends Number 

输入图像描述这里

T – 用于表示类型
E – 用于表示元素
K – 键
V - 值
N – 数字
参考:

输入图像描述这里

<?> is a shorthand for <? extends Object>.
You may read below shared link for more details.


<?>

"?" denotes any unknown type, It can represent any Type at in code for. Use this wildcard if you are not sure about Type.

ArrayList<?> unknownList = new ArrayList<Number>(); //can accept of type Number
unknownList = new ArrayList<Float>(); //Float is of type Number

Note: <?> means anythings. So It can accept of Type which are not inherited from Object class.

<? extends Object>

<? extends Object> means you can pass an Object or a sub-class that extends Object class.

ArrayList<? extends Number> numberList = new ArrayList<Number>(); //Number of subclass
numberList = new ArrayList<Integer>(); //Integer extends Number
numberList = new ArrayList<Float>(); // Float extends Number 

enter image description here

T – used to denote type
E – used to denote element
K – keys
V - values
N – for numbers
Ref:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文