当前位置: 移动技术网 > IT编程>开发语言>Java > java.lang.Void类的解析与使用详解

java.lang.Void类的解析与使用详解

2019年07月19日  | 移动技术网IT编程  | 我要评论
今天在查看源码的时候发现了 java.lang.void 的类。这个有什么作用呢? 先通过源码查看下 package java.lang; /** * t

今天在查看源码的时候发现了 java.lang.void 的类。这个有什么作用呢?

先通过源码查看下

package java.lang;
/**
 * the {@code void} class is an uninstantiable placeholder class to hold a
 * reference to the {@code class} object representing the java keyword
 * void.
 *
 * @author unascribed
 * @since  jdk1.1
 */
public final
class void {
  /**
   * the {@code class} object representing the pseudo-type corresponding to
   * the keyword {@code void}.
   */
  @suppresswarnings("unchecked")
  public static final class<void> type = (class<void>) class.getprimitiveclass("void");

  /*
   * the void class cannot be instantiated.
   */
  private void() {}
}

从源码中发现该类是final的,不可继承,并且构造是私有的,也不能 new。

那么该类有什么作用呢?

下面是我们先查看下 java.lang.integer 类的源码

我们都知道 int 的包装类是 java.lang.integer

 

从这可以看出 java.lang.integer 是 int 的包装类。

同理,通过如下 java.lang.void 的源码可以看出 java.lang.void 是 void 关键字的包装类。

public static final class<void> type = (class<void>) class.getprimitiveclass("void");

void 使用

void类是一个不可实例化的占位符类,如果方法返回值是void类型,那么该方法只能返回null类型。

示例如下:

public void test() { 
  return null;
}

使用场景一:

future<void> f = pool.submit(new callable() {
  @override
  public void call() throws exception {
    ......
    return null;
  }
    
});

比如使用 callable接口,该接口必须返回一个值,但实际执行后没有需要返回的数据。 这时可以使用void类型作为返回类型。

使用场景二:

通过反射获取所有返回值为void的方法。

public class test {
  public void hello() { }
  public static void main(string args[]) {
    for (method method : test.class.getmethods()) {
      if (method.getreturntype().equals(void.type)) {
        system.out.println(method.getname());
      }
    }
  }
}

执行结果:

main
hello
wait
wait
wait
notify
notifyall

ps:下面介绍java.lang.void 与 void的比较及使用

void关键字表示函数没有返回结果,是java中的一个关键字。

java.lang.void是一种类型。例如给void引用赋值null。

void nil = null;

通过void类的代码可以看到,void类型不可以继承与实例化。

public final
class void {
  /**
   * the {@code class} object representing the pseudo-type corresponding to
   * the keyword {@code void}.
   */
  @suppresswarnings("unchecked")
  public static final class<void> type = (class<void>) class.getprimitiveclass("void");

  /*
   * the void class cannot be instantiated.
   */
  private void() {}
}

void作为函数的返回结果表示函数返回null(除了null不能返回其它类型)。

 void function(int a, int b) {
  //do something
  return null;
 }

在泛型出现之前,void一般用于反射之中。例如,下面的代码打印返回类型为void的方法名。

public class test {
  public void print(string v) {}
  public static void main(string args[]){
    for(method method : test.class.getmethods()) {
      if(method.getreturntype().equals(void.type)) {
        system.out.println(method.getname());
      }
    }
  }
}

泛型出现后,某些场景下会用到void类型。例如future<t>用来保存结果。future的get方法会返回结果(类型为t)。

但如果操作并没有返回值呢?这种情况下就可以用future<void>表示。当调用get后结果计算完毕则返回后将会返回null。

另外void也用于无值的map中,例如map<t,void>这样map将具set<t>有一样的功能。

因此当你使用泛型时函数并不需要返回结果或某个对象不需要值时候这是可以使用java.lang.void类型表示。

总结

以上所述是小编给大家介绍的java.lang.void的类 解析与使用详解,希望对大家有所帮助

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网