java中的合法主方法签名 [英] legal main method signature in java

查看:171
本文介绍了java中的合法主方法签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class NewClass{
public static void main(String a){
    System.out.print("Hello");
}
}

当我尝试执行上面的代码时,那么它显示错误,找不到主要方法。但当我将 public static void main(String a) 更改为 public static void main(String .. .a) public static void main(String a []) 。然后,它工作.. !!

When I'm trying to execute above code, then it shows an error, main method not found. But when I changed public static void main(String a) to public static void main(String... a) or public static void main(String a[]). Then, it works..!!

所以,我的问题是我们可以用多少不同的方式编写法律主要方法签名以及此签名 public static void main(String ... a) 是什么意思?

So, My question is how many different ways we can write legal main method signature and what this signature public static void main(String... a) means ?

推荐答案

仅仅因为这是Java的要求。

Simply because that's the requirement of Java.

主要方法/入口点必须是一个声明为 public static void main(String [] args)的方法。使用 String 参数声明的方法类似但不兼容。

A main method/entry point to a program must be a method declared as public static void main(String[] args). Your method that was declared with a String parameter was similar but not compatible.

数组与单个字符串不同 - 如果有人用三个命令行参数调用Java,JVM会创建一个三元素字符串数组,然后如何将它传递给只需要一个字符串的方法?

An array is not the same as a single String - if someone invoked Java with three command-line parameters, the JVM would create a three-element string array, and then how would it pass this into your method that only takes a single string?

所以在这种情况下你试图基于的类启动一个Java程序没有主要方法作为入口点。

So in that case you were trying to launch a Java program based on a class that did not have an main method to act as an entry point.

(之所以 String ... works是因为这是数组参数的语法糖,并编译为具有相同签名的方法。)

(The reason why String... works is because this is syntactic sugar for an array parameter, and compiles down to a method with the same signature.)

这篇关于java中的合法主方法签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆