本文介绍了Java泛型中的Void和无界通配符有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java泛型中,Void类型的对象与无界通配符类型的对象之间有什么区别?我的意思是我理解了<>的用法,并且还使用了反射方面的Void,但是当我看到

的Java源代码时,我有点感兴趣

  java.util.concurrent.AbstractExecutorService 

及其方法

  public Future<?>提交(Runnable任务){
...
RunnableFuture< Void> ftask =新的TaskFor(task,null);
...
return ftask;

其中内部使用RunnableFuture< Void>而不是RunnableFuture<?>



有人能帮我理解背后的原因吗?谢谢

解决方案

是一个特殊的类,用于表示 no 返回值。虽然 Void 本身并没有什么特别之处,但它不能(也从不)被实例化,所以唯一可能的值是总是 null 。它被用于两件事:


  1. 说一个泛型方法或类没有返回值。
  2. 使用 Void.TYPE 在Java反射中表示 void 返回类型。例如,。

  3. ol>

    所以它与通配符完全不同,它不是实际的类,而是在编译时代表一个特定的 unknown 类型。在运行时,它与其他所有泛型类型一样。




    关于提交方法。以下是JDK 6的两个实现:

      public Future<?> submit(Runnable task){
    if(task == null)throw new NullPointerException();
    RunnableFuture< Object> ftask = newTaskFor(task,null);
    执行(ftask);
    返回ftask;
    }

    和JDK 7:

      public Future<?> submit(Runnable task){
    if(task == null)throw new NullPointerException();
    RunnableFuture< Void> ftask = newTaskFor(task,null);
    执行(ftask);
    返回ftask;

    $ / code>

    正如您所看到的,类型已更改为 Void 仅适用于JDK 7,可能是因为它在概念上更有意义。但是由于该方法的接口不能被改变(出于兼容性的原因,并且因为该方法实现了接口),返回类型未来<?> 保持不变。这是我的解释。


    What is the difference between an object of a Void type and that of an unbounded wildcard type in Java generics? I mean I understand the use of <?>, and also the use of Void in terms of reflection, but I was a bit intrigued when I saw the Java source code for

    java.util.concurrent.AbstractExecutorService
    

    and its method

    public Future<?> submit(Runnable task) {
        ...
        RunnableFuture<Void> ftask = new TaskFor(task, null);
        ...
        return ftask;
    

    where inside the method it uses a RunnableFuture<Void> instead of RunnableFuture<?>

    can someone help me understand the reason behind this? Thanks

    解决方案

    Void is a special class that is used to represent no return value. While there is nothing special about Void per se, it can't be (and never is) instantiated, so the only possible value for it is always null. It is used for two things:

    1. To say that a generic method or class has no return value.
    2. To represent the void return type in Java reflection using Void.TYPE. See How to determine by reflection if a Method returns 'void' for example.

    So it is very different from a wildcard, which is no actual class but represents one specific, unknown type at compile time. At runtime, it is erased like every other generic type.


    Regarding the submit method. Here are the two implementations from JDK 6:

    public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Object> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
    

    and JDK 7:

    public Future<?> submit(Runnable task) {
        if (task == null) throw new NullPointerException();
        RunnableFuture<Void> ftask = newTaskFor(task, null);
        execute(ftask);
        return ftask;
    }
    

    As you can see, the type was changed to Void only in JDK 7, probably because it makes more sense conceptually. But since the interface of the method could not be changed (for compatibility reasons and because the method implements Future<?> submit(Runnable task) of the ExecutorService interface), the return type Future<?> stayed the same. That's my explanation.

    这篇关于Java泛型中的Void和无界通配符有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 22:45