简体   繁体   English

Java初学者关于主方法中String [] args的问题

[英]Java Beginner question about String[] args in the main method

So I just tried excluding String[] args from the main method 所以我只是尝试从main方法中排除String[] args

It compiled alright ! 它汇编好了!

But JVM is showing an exception 但JVM正在显示异常

Why did it compile when String[] args HAS to be included every time ? 为什么每次包含String[] args时都要编译?

What is going on here ? 这里发生了什么 ? Why won't it show a compilation error ? 为什么不显示编译错误?

typing this made me think that may be compiler did not see it as THE main method ..is that so ? 打字这让我觉得可能是编译器没有把它看成是main方法..是这样的吗?

If that is the case..why not ? 如果是这样的话......为什么不呢? I mean isn't there supposed to be just one main method that MUST have String[] args as the argument ? 我的意思是不应该只有一个主要的方法必须有String[] args作为参数?

The JVM is looking for a very special main method to run. JVM正在寻找一种非常特殊的主要方法来运行。 Only the signature 只有签名

public static void main( String[] args )

is found. 找到了。 All other methods with name main are just "normal" methods. 名为main所有其他方法都只是“普通”方法。

typing this made me think that may be compiler did not see it as THE main method ..is that so ? 打字这让我觉得可能是编译器没有把它看成是主要方法..是这样的吗?

Correct. 正确。 There is no compile error because you're perfectly free to have all kinds of methods named main . 没有编译错误,因为您可以完全自由地使用名为main各种方法。 But when you start the JVM and give it a "main class", then it will look for a method static public void main(String[]) in that class, and if it does not find such a method, it aborts with an exception. 但是当你启动JVM并给它一个“主类”时,它会在该类中查找一个方法static public void main(String[]) ,如果它没有找到这样的方法,它将以异常中止。

This allows you to have multiple main methods in your program and is really the only thing that makes sense if you think about it: Applications can be composed from classes and JAR files from lots of different sources, written by different people at different times, so in many cases you can't really have a single, designated "main class" right from the start. 这允许你在你的程序中有多个主要方法,并且如果你考虑它是唯一有意义的:应用程序可以由来自不同来源的不同来源的不同来源的类和JAR文件组成,所以在许多情况下,你从一开始就不能真正拥有一个指定的“主类”。

Not every class has to have a public static void main(String[] args) method, only the one you want to run from the JVM. 并非每个类都必须有一个public static void main(String[] args)方法,只有你想要从JVM运行的方法。 The result is that your class will compile with no error if it finds public static void main() but will throw an exception if you try and run it with the JVM because it can not find the entry point of the program. 结果是如果找到public static void main() ,你的类将编译时没有错误,但是如果你尝试用JVM运行它会抛出异常,因为它找不到程序的入口点。

Bottom line the entry point of your program must be public static void main(String[] args) which must be located in at least one of your .java files. 底线程序的入口点必须是public static void main(String[] args) ,它必须位于至少一个.java文件中。

Note you can have multiple public static void main(String[] args) methods in your code (one per class) the advantage is you can use these to test and debug your classes individually. 请注意,您的代码中可以有多个public static void main(String[] args)方法(每个类一个),您可以使用这些方法单独测试和调试类。

To try to answer "why is it legal to compile without a proper main method" it's because not every java project is a stand alone application that can be run. 试图回答“为什么没有适当的主方法进行编译是合法的”,这是因为不是每个java项目都是可以运行的独立应用程序。 Some are just libraries, where other programs will include them as jar files and use their code, but they don't "run" themselves. 有些只是库,其他程序将它们包含在jar文件中并使用它们的代码,但它们不会自行“运行”。 Others may be web applications, where they are deployed onto a web server that has already been started, only the server itself actually has a proper "main" method. 其他可能是Web应用程序,它们被部署到已经启动的Web服务器上,只有服务器本身实际上具有适当的“主”方法。 The web application project is opened up and executed by it. Web应用程序项目由它打开并执行。

The compiler didn't really know at compile time that you were intending to try to run your code as a stand along application. 编译器在编译时并不真正知道您打算尝试将代码作为应用程序的代码运行。

Java supports method overloading. Java支持方法重载。 This means you can have several methods with the same name, but different arguments. 这意味着您可以使用多个具有相同名称但不同参数的方法。

Having said that, when you run java ClassName , Java looks in ClassName.class for a method with the signature public static void main (String[]) (it doesn't care what the String[] variable is named) and runs it. 话虽如此,当你运行java ClassName ,Java会在ClassName.class查找带有签名public static void main (String[]) (它不关心String[]变量的名称)并运行它。 If it doesn't find one, Java will bomb out with the following exception: 如果找不到,Java会弹出以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: main 线程“main”中的异常java.lang.NoSuchMethodError:main

You are correct. 你是对的。 The runtime is looking for a main method that takes a string array as a parameter, and isn't finding it. 运行时正在寻找一个将字符串数组作为参数的主方法,而不是找到它。

The fact that you have a main method that doesn't take a string array is irrelevant. 你有一个不带字符串数组的main方法的事实是无关紧要的。 just like any other method, you can create multiple versions of main() that take different parameters - the runtime will just ignore them. 就像任何其他方法一样,您可以创建具有不同参数的main()多个版本 - 运行时将忽略它们。

Isn't that overloading? 是不是超载? It's fully legal to define a method 定义方法是完全合法的

static void main() {
}

It's just not the entry point the JVM will be looking for. 这不是JVM将要寻找的切入点。

Overloading is the ability to have multiple methods with the same name but different arguments . 重载是指具有相同名称但不同参数的多个方法的能力。 The compiler in fact creates a name based on the method name and the arguments. 实际上,编译器根据方法名称和参数创建名称。

So a main(String[]) would be called, to the compiler, something like main_String_arr, and main() would be called something like main. 因此,对于编译器,将调用main(String []),类似于main_String_arr,而main()将被称为main。

Yes.. 是..

The Java compiler will look for the same method signature to consider it a main Java编译器将查找相同的方法签名 ,将其视为main

Writing any function that has the same name but another parameters will result in overloading functions.. 编写任何具有相同名称但具有其他参数的函数将导致重载函数。
Overloaded functions are not the same..!! 重载功能不一样.. !!

The case in C# is somehow different.. C#中的情况有点不同..

At last, you must make sure that your main is like that: 最后,你必须确保你的主要是这样的:

 public static void main(String[] args) 

You can have many methods named main, but only one can be THE main one - entry point to the program. 你可以有许多名为main的方法,但只有一个可以是主要的 - 程序的入口点。 It is the one with String[] args. 它是String [] args的那个。

It's still a valid method. 它仍然是一种有效的方法。 For example, you could have a static method called "main" with an int parameter if you wanted: 例如,如果您需要,可以使用带有int参数的名为“main”的静态方法:

public static void main(int foo){}

The compiler doesn't complain because it's a valid method! 编译器没有抱怨,因为它是一个有效的方法! It's just that, in order to run a Java program, Java looks for a static method called "main" with a single String array argument. 只是,为了运行Java程序,Java寻找一个名为“main”的静态方法,该方法带有一个String数组参数。

You can use the compiler to compile part of an application: eg to make a jar file which you will call from another part of the app; 您可以使用编译器来编译应用程序的一部分 :例如,制作一个jar文件,您将从应用程序的另一部分调用该文件; or an applet that is started in a different way. 或者以不同方式启动的applet。 Therefore the compiler cannot complain about the lack of a main(String[]) method. 因此编译器不能抱怨缺少main(String [])方法。 But trying to run the results of such a compile. 但是试图运行这样的编译结果。

When you try to run the results of such a compile java is always looking for a specific main(String[]); 当你尝试运行这样的编译结果时,java总是在寻找一个特定的main(String []); if it can't find it it will throw a runtime exception. 如果它找不到它会抛出一个运行时异常。 The main used to start an app must have exactly that signature. 主要用于启动应用程序必须正是签名。

Here's a quote from Java Tutorials/Getting Started : 以下是Java Tutorials / Getting Started的引用:

In the Java programming language, every application must contain a main method whose signature is: 在Java编程语言中,每个应用程序都必须包含一个main方法,其签名为:

 public static void main(String[] args) 

The modifiers public and static can be written in either order ( public static or static public ), but the convention is to use public static as shown above. 修饰符publicstatic可以按任意顺序编写( public staticstatic public ),但约定是使用public static如上所示。 You can name the argument anything you want, but most programmers choose " args " or " argv ". 您可以根据需要为参数命名,但大多数程序员选择“ args ”或“ argv ”。

The main method is similar to the main function in C and C++; main方法类似于C和C ++中的主要功能; it's the entry point for your application and will subsequently invoke all the other methods required by your program. 它是您的应用程序的入口点,随后将调用您的程序所需的所有其他方法。

So to be precise, the main method is defined by Java as an application entry point. 因此,要确切地说 main方法由Java作为一个应用程序的入口点定义的。 Not everything is an application, and not every main method is an application entry point. 并非所有内容都是应用程序,并非每个main方法都是应用程序入口点。 Applets, for example, don't need a main , because it's started with a different mechanism. 例如,Applet不需要main ,因为它是以不同的机制开始的。

Note also that since the introduction of varargs, you can actually declare your application entry point as follows: 另请注意,自引入varargs以来,您实际上可以按如下方式声明应用程序入口点:

 public static void main(String... args)

This works because underneath the syntactic sugar, varargs in Java is implemented as arrays. 这是有效的,因为在语法糖的下面,Java中的varargs被实现为数组。

See also 也可以看看

Related questions 相关问题

Important points: 重点:

  1. Following is the signature of the main method: 以下是主要方法的签名:

     public static void main(String[] args) 
  2. main method can be overloaded. main方法可以重载。

  3. Multiple classes can contain the main method inside a single compilation unit (and so all of them will be called executable classes) 多个类可以在单个编译单元中包含main方法(因此所有这些都将被称为可执行类)

  4. The class containing the main method may or may not be public. 包含主要方法的类可能是也可能不是公开的。

  5. By mistake, if you were to omit the static keyword (or the signature differs in any way), compilation would be done but a runtime error will occur. 错误地,如果您要省略static关键字(或签名以任何方式不同),将完成编译但会发生运行时错误。

From my blog: 来自我的博客:

Java: Important points about the main method Java:关于main方法的重点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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