java中的“main”可以返回一个字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23856710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can "main" in java return a String?
提问by Jeren
Is it possible that public static void main(String[] args) in java returns String
instead of void
? If yes, how?
java中的public static void main(String[] args)是否有可能返回String
而不是void
?如果是,如何?
public static String main(String[] args)
instead of:
代替:
public static void main(String[] args)
when I change my code as below:
当我改变我的代码如下:
public static String main(String[] args) throws IOException {
String str = null;
TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
str = new Stm(parser).parse("bizler");
System.out.println("str = " + str);
String replace = str.replace("[","");
String replace1 = replace.replace("]","");
List<String> result1 = new ArrayList<String>(Arrays.asList(replace1.split(",")));
String result = result1.get(0);
System.out.println("Result = " + result);
return result;
}
I receive this error:
我收到此错误:
Error: Main method must return a value of type void in class Stm, please define the main method as:
public static void main(String[] args)
回答by Mureinik
In short - no, it can't.
You can always print to stdout from the main method (using System.out.print
or System.out.println
), but you can't change the return type of main
.
简而言之 - 不,它不能。您始终可以从 main 方法(使用System.out.print
或System.out.println
)打印到标准输出,但不能更改main
.
回答by Aviad
No you can't... once the main is finished the program is dead.. So you don't have any benefit from that.. What is you purpose? What you are trying to achieve?
不,你不能......一旦主程序完成,程序就死了......所以你没有任何好处......你的目的是什么?你想要达到什么目标?
You can wrap all in other method that will return String to your main.
您可以将所有内容包装在其他方法中,这些方法会将 String 返回到您的主要内容。
public static void main(String[] args) throws IOException {
String result = doSomething();
return result;
}
public static String doSomething() {
String str = null;
TurkishMorphParser parser = TurkishMorphParser.createWithDefaults();
str = new Stm(parser).parse("bizler");
System.out.println("str = " + str);
String replace = str.replace("[","");
String replace1 = replace.replace("]","");
List<String> result1 = new ArrayList<String>(Arrays.asList(replace1.split(",")));
String result = result1.get(0);
System.out.println("Result = " + result);
}
回答by coder hacker
Yes you can but you can't run that class. You will get error
是的,你可以,但你不能运行那个类。你会得到错误
class Test {
public static String main(String[] args) {
return "1";
}
}
You will get error as
你会得到错误
Error: Main method must return a value of type void in class Test, please
define the main method as:
public static void main(String[] args)
回答by Bohemian
No. The to be a main()
method, it must return nothing (ie be void
).
不。要成为一个main()
方法,它必须不返回任何内容(即 be void
)。
However, you could refactor your code if you need the functionality of your method returning something:
但是,如果您需要方法的功能返回某些内容,则可以重构代码:
public static void main(String[] args) throws IOException {
myMain(args);
}
public static String myMain(String[] args) throws IOException {
// your method, which can now be called from anywhere in your code
}
回答by kviiri
Yes, it's definitely possible. That is, you can define a method public static String main(String[] args)
. It's just like defining any other method.
是的,这绝对有可能。也就是说,您可以定义一个方法public static String main(String[] args)
。这就像定义任何其他方法一样。
However, it won't be a main methoddespite its name, and thus won't be run when executing a program like a main method would. Quoting Java language specification, §12.14:
但是,尽管它的名称是 main 方法,但它不会是 main 方法,因此在执行程序时不会像 main 方法那样运行。引用 Java 语言规范,§12.14:
The method main must be declared public, static, and void.
方法 main 必须声明为 public、static 和void。
Emphasis mine. You can't have non-void main methods in Java.
强调我的。Java 中不能有非空的 main 方法。
回答by René Link
回答by Mohit Kanwar
This is a very interesting scenario. While in general, we can change any method which is returning void to return anything else without much impact, main method is a special case.
这是一个非常有趣的场景。通常,我们可以将任何返回 void 的方法更改为返回任何其他内容而不会产生太大影响,但 main 方法是一种特殊情况。
In earlier programming languages, the return from main method was supposed to return exit values to the OS or calling environment.
在早期的编程语言中,main 方法的返回应该是将退出值返回给操作系统或调用环境。
But in case of Java (where multi-threading concept case into picture), returning a value from main method would not be right, as the main method returns to JVM instead of OS. JVM then finishes other threads e.g. deamon threads (if any) and performs other exit tasks before exit to OS.
但是在 Java 的情况下(多线程概念案例),从 main 方法返回一个值是不正确的,因为 main 方法返回到 JVM 而不是 OS。JVM 然后完成其他线程,例如守护线程(如果有)并在退出到操作系统之前执行其他退出任务。
Hence allowing main method to return, would have lead to a false expectation with the developers. hence it is not allowed.
因此,允许 main 方法返回,将导致开发人员的错误期望。因此是不允许的。
回答by smttsp
If you really, really need it to return something (which you don't), assign the output to a static variable. Then you won't need to return. i.e
如果你真的,真的需要它返回一些东西(你不需要),请将输出分配给一个静态变量。那你就不用回来了。IE
static String a = "";
public static void main(String[] args){
//do sth
a = "whatever you want";
}
Now you have String a
, use it whereever you want to use but I don't see any usage for this.
现在你有了String a
,在任何你想使用的地方使用它,但我没有看到任何用途。
回答by vanrado
Answer is no.
答案是否定的。
When the program is run, the JVM looks for a method named main() which takes an array of Strings as input and returns nothing (i.e. the return type is void). But if it doesn't find such a method ( the main() method is returning Stringfor example ) so it throws a java.lang.NoSuchMethodError
当程序运行时,JVM 查找名为 main() 的方法,该方法将字符串数组作为输入,并且不返回任何内容(即返回类型为 void)。但是如果它没有找到这样的方法(例如main() 方法返回 String),那么它会抛出一个java.lang.NoSuchMethodError
回答by Vikas
Before java, in C or C++, main function could be declared int or void. Why? Because main method is invoked by OS which is not responsible for the successful/unsuccessful execution of program. So, to let the OS know that the program executed successfully we provide a return value.
在 java 之前,在 C 或 C++ 中,main 函数可以声明为 int 或 void。为什么?因为 main 方法是由操作系统调用的,它不负责程序的成功/失败执行。因此,为了让操作系统知道程序成功执行,我们提供了一个返回值。
Now, in java, program is loaded by OS, but the execution is done by JRE. JRE itself is responsible for the successful execution of program. So, no need to change the return type.
现在,在java中,程序由操作系统加载,但执行由JRE完成。JRE 本身负责程序的成功执行。因此,无需更改返回类型。
It will be like telling your problems to god, when god himself is giving you problems to solve them.
这就像把你的问题告诉上帝,而上帝自己却在给你问题来解决它们。
;)
;)