0

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) {
1
  • 3
    If you use later one than you will get exception: java.lang.NoSuchMethodError: main Exception in thread "main" Commented Jan 2, 2012 at 11:17

8 Answers 8

9

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

You mean an array of Strings, not a String of arrays.
1

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.

1 Comment

String[] is an array of String classes — simply not true. It's an array of instances.
1

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.

Comments

0

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).

Comments

0

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.

Comments

0

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).

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.