简体   繁体   English

Java中的主要方法仅接受String []

[英]Main method in java only accept String[]

Object is the super type of all classes in Java . ObjectJava中所有类的超级类型。 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从不给这个机会,而objectjava所有类的最终晚餐类型。

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 有几种方法可以解决这个问题

  1. "because". “因为”。 the main() entry point to a program is specified like this and canot be overloaded to accept other arguments 像这样指定程序的main()入口点,并且可以重载以接受其他参数
  2. as hinted by assylias, the main() method is expected to be invoked from a comamnd line or shell. 正如assylias所暗示的那样,main()方法应该从共行或shell调用。 this means that the arguements will always be strings. 这意味着争论将永远是字符串。 whatever you type on a command line is a string. 您在命令行上键入的任何内容都是字符串。

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. 无法从命令行输入对象。

From JLS : JLS

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使限制传递字符串变量。

  1. JVM calls main() as a thread. JVM将main()作为线程调用。 When we call main() from another method in java, we call it as function or method. 当我们从Java中的另一个方法调用main()时,我们将其称为函数或方法。
  2. Actually the confusion here is " why auto casting is not applicable when we try to call main(Object args) from console." 实际上,这里的困惑是“为什么当我们尝试从控制台调用main(Object args)时自动投射不适用”。 May be this restriction has been put in native methods of JVM to avoid the complications. JVM的本机方法中可能已添加了此限制,以避免出现复杂情况。
  3. I you guys observe same case u will find for catch(Object e) Summery line: it is the native methods code of JVM which restricts the auto casting, to avoid complications. 我你们观察到您会为catch(Object e)Summery这一行找到相同的情况:它是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM