如图
运行则报错
Error:(43, 47) java: 不兼容的类型: void无法转换为java.lang.Thread
换成
则没有问题,也能正常运行。
这是什么原因?
new Thread().start();
和
Thread thread = new Thread();
thread.start();
不等价吗?构造器为什么会返回void。。。void的话为什么第二种方式没问题。。
补充:JDK 1.8
new Thread().start()返回的是start方法的返回值,而start返回值类型是void(无返回值类型)
楼上说的对,,补充一下
new Thread().start();
和
Thread thread = new Thread();
thread.start();
等价,,,
但是
Thread thread = new Thread();
thread.start();
和
Thread thread = new Thread().start();
不等价
原因正是【new Thread().start()返回的是start方法的返回值,而start返回值类型是void(无返回值类型)】(楼上那句话)