//为什么当我调用 hello 方法时会显示编译时错误?
public class Test {
public static void main(String [] args) {
System.out.println(hello());
}
public static void hello() {
System.out.println("from hello");
}
}
最佳答案
因为 hello()
不会返回 String
、Object
或各种 println 接受的任何其他类型
PrintStream
中定义的方法.
事实上,您可以将任何类型的参数传递给 println
并获得某种形式的输出,但由于方法的返回类型被声明为 void
,您实际上并没有传递参数。
您可以通过将 hello()
方法更改为类似以下内容来消除错误:
public static String hello() {
return "Hello world";
}
或者将方法调用更改为:
System.out.println();
关于java - 核心 Java 中的编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310542/