[英]Main method in java only accept String[]
Object
is the super type of all classes in Java
. Object
是Java
中所有类的超级类型。 Consider my following class 考虑我的下课
public class Test {
public static void main1(Object[] args) {
System.out.println("I accept an object array");
}
public static void main(String[] args) {
main1(args);
}
}
Due to object
superiority object
array can accept any object
type arrays. 由于
object
优越性, object
数组可以接受任何object
类型数组。 But Still java
doesn't consider following class contains a main method. 但是,
java
仍然不考虑以下类包含main方法。
public class Test {
public static void main(Object[] args) {
}
}
Why java
never give this opportunity while object
is ultimate supper type for all classes in java
. 为什么
java
从不给这个机会,而object
是java
所有类的最终晚餐类型。
The main Method of Java is specified with strings as parameters. Java的主要方法以字符串作为参数来指定。 So the compiler can't detect the main-method with objects as args.
因此,编译器无法将对象作为args检测到主方法。 I guess the resaon for this is, that the main method are usally called due an console command, and there you have only the opportunity to set strings as parameters.
我想这是因为控制台命令通常会调用main方法,并且您只有机会将字符串设置为参数。
because java looks explicitly for public static void main(String[] args)
when running. 因为Java在运行时会明确查找
public static void main(String[] args)
。 specified in 12.1.4 of the jls 在jls的12.1.4中指定
The method main must be declared public, static, and void.
方法main必须声明为public,static和void。 It must specify a formal parameter (§8.4.1) whose declared type is array of String.
它必须指定一个声明的类型为String数组的形式参数(第8.4.1节)。 Therefore, either of the following declarations is acceptable:
因此,可以使用以下任何一种声明:
Object wouldn't make sense, because you can not pass an other object through the console. 对象没有意义,因为您无法通过控制台传递其他对象。
there are a few ways to answer this 有几种方法可以解决这个问题
The String[]
were for command-line arguments, strings are what the user enters at the command line. String[]
用于命令行参数,字符串是用户在命令行输入的内容。 Objects can't be entered from Command line. 无法从命令行输入对象。
The method main must be declared public, static, and void.
方法main必须声明为public,static和void。 It must specify a formal parameter whose declared type is array of String.
它必须指定一个声明形式为String数组的形式参数。 Therefore, either of the following declarations is acceptable:
因此,可以使用以下任何一种声明:
public static void main(String[] args)
public static void main(String... args)
One point as all explain there is no way to pass object from console so it's meaningless. 所有人都解释了一点,因为没有办法从控制台传递对象,所以毫无意义。
Still as I also think Object is super class so why jvm does not understand it, but there is also other point that if jvm allowed to accept Object arguments than user can pass non-string variable as well so there jvm will create error that's why I think jvm make restrict to pass string variable. 我仍然认为Object是超类,所以jvm为什么不理解它,但是还有一点,如果jvm允许接受Object参数,那么用户也可以传递非字符串变量,因此jvm会产生错误,这就是为什么认为jvm使限制传递字符串变量。
The arguments passed to the main method are from command line. 传递给main方法的参数来自命令行。 So they are
String
所以他们是
String
main method can also be written like this 主要方法也可以这样写
public static void main(String... args) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.