您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java.lang.Void和void的使用方法比较

2024/5/6 15:11:22发布32次查看
这篇文章主要介绍了java.lang.void 与 void的比较及使用方法介绍,小编觉得挺不错的,这里给大家分享一下,需要的朋友可以参考。
void不是函数,是方法的修饰符,void的意思是该方法没有返回值,意思就是方法只会运行方法中的语句,但是不返回任何东西。
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类型。例如future8742468051c85b06f0a0af9e3e506b5c用来保存结果。future的get方法会返回结果(类型为t)。
但如果操作并没有返回值呢?这种情况下就可以用future903bf37051cf83cbd9686768ac0189ae表示。当调用get后结果计算完毕则返回后将会返回null。
另外void也用于无值的map中,例如mapbd1f348043883594477738d6b6495f86这样map将具set8742468051c85b06f0a0af9e3e506b5c有一样的功能。
因此当你使用泛型时函数并不需要返回结果或某个对象不需要值时候这是可以使用java.lang.void类型表示。
总结
以上就是java.lang.void和void的使用方法比较的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product