Some Java Guy
Some Java Guy

Reputation: 5118

Difference between String[] and String

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

Upvotes: 0

Views: 3357

Answers (8)

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

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.

Upvotes: 9

Abhishek bhutra
Abhishek bhutra

Reputation: 1422

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.

Upvotes: 0

Tudor
Tudor

Reputation: 62469

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

Upvotes: 0

jenaiz
jenaiz

Reputation: 547

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.

Upvotes: 0

TCoder
TCoder

Reputation: 19

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.

Upvotes: 1

Juha Laiho
Juha Laiho

Reputation: 596

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

Upvotes: 0

aleroot
aleroot

Reputation: 72686

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.

Upvotes: 1

npinti
npinti

Reputation: 52205

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.

Upvotes: 1

Related Questions