Вопрос

I was just wondering the difference between String[] and String in main method

 public static void main(String[] args) {

VS

 public static void main(String args) {
Это было полезно?

Решение

String[] = array of strings

String = single string...

The main method of a program you'll run via the java command-line tool must have String[] as its only argument. The strings in the array are the command-line arguments.

Другие советы

The main method in Java takes only an Array of Strings:

The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

Taken from here.

I think that you are looking at an overloaded method of the main method, something which was created by someone else and is not the actual entry point of the application.

String[] is an array of String classes while String is an instance of String class.

The main method in Java require an array of string as parameter.

String[] is an array of strings while String is a single string , you can pass more than one argument to the main function so you have to use String[] and not String.

Former can be used as the entry point for programs launched from operating system; the latter cannot (it can just be called from other methods).

If you want to execute you class you need to respect the first way. The second way without using array don't allow you to execute your class.

When you execute your program the main method is called and the command-line arguments are passed as individual strings inside the String array which is the argument of main (first case).

It's easier to manage than just passing the entire argument list as a single string (second case) and then having to parse it somehow (you can't build your program like this anyway).

No such method if you are thinking to execute class using main

 public static void main(String args) {

String[] is used to signify that the user may opt to enter parameters to the java program at command line. We can use both String[] args or String args[]. The Java compiler would accept both forms.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top