Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use String [] args in Java main method

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Java main method of String [] args how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone read this Java main method of String [] args how to use the article will have a harvest, let's take a look at it.

Every student studying Java should have written the following code. Public class Hello {public static void main (String [] args) {System.out.println ("Hello,World!");}}

In the Java language, it is specified that the entry function is a main () method that takes a parameter of type String [], but in many cases, many people don't use this parameter and don't care why they write it.

So what exactly are the parameters of this string array type?

In fact, it is very simple. The parameters of the string array type in the main () method are the parameters of the java command. Running the main () method using the java command will input the parameters of the java command into the string array parameters of the Java main () method.

We can verify it in the following ways:

1. Write a Hello.java file with the following contents:

Public class Hello {public static void main (String [] args) {System.out.println ("= args start="); for (int I = 0; I

< args.length; i++) { System.out.println(args[i]); } System.out.println("==============args end============"); }} 2、在Hello.java文件的路径下打开cmd命令提示符,运行javac Hello.java命令编译该文件,这将会在对应的文件路径下,得到一个Hello.class字节码文件。 3、使用java Hello命令运行Hello.class文件,我们将会得到如下的运行结果:

4. This time we add some parameters after the java command, which we can define by ourselves. For example: java Hello a b c d, we will get the following running result:

It is precisely because of this extensibility of the Java main () method that each developer can achieve some different functions by defining some parameters of the Java command.

The startup class of SpringBoot actually uses this feature. The SpringApplication.run () method has an overloaded method, in which the first input parameter is the bytecode object of the class annotated with @ SpringBootApplication, and the second parameter is the string array parameter of the main () method.

Through this string array parameter, SpringBoot can implement different application configurations in different environments by means of java command parameters.

@ SpringBootApplicationpublic class MyApplication {public static void main (String [] args) {SpringApplication.run (MyApplication.class, args);}} this is the end of the article on "how to use String [] args in the Java main method. Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use String [] args in the Java main method. If you want to learn more, you are welcome to follow the industry information channel."

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report