It's not a secret anymore!
The moose likes Java in General and the fly likes concept in main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "concept in main()" Watch "concept in main()" New topic

concept in main()

Soumya Padhiary

Joined: Jan 10, 2013
Posts: 20

i know that, in java main method is called by JVM as thread.
but when i mentioned main method in a progrme, like below, it successfully run:
public static void main(String... args)
System.out.println("in main");
instead of String[], is it acceptable ?
what is the differencr between (String... s) and (String[] args) ?
please give answer ,
thanks in advance .
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

Its called varargs, equivalent to Java array, added in Java 5.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Manish Dubey J

Joined: Oct 08, 2011
Posts: 9

Hi Soumya,

""what is the differencr between (String... s) and (String[] args) ?""

This is a different concept of 'varargs'. It has nothing to do with main method!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Soumya Padhiary wrote:
what is the differencr between (String... s) and (String[] args) ?

with String[] argument you need pass the appropriate argument as new String[]{"values"}

with String... you no need to, compiler do array creation and appropriate method call.
here even method call with no argument is possible as compiler insert zero size appropriate array .
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15082

Soumya Padhiary wrote:instead of String[], is it acceptable ?

Yes. Section 12.1.4 of the Java Language Specification states:
JLS wrote:
Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.

The method main must be declared public, static, and void. It must specify a formal parameter (ยง8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Start reading about varargs here. You are allowed [] or ... in the main method, as it says here.
I agree. Here's the link:
subject: concept in main()
It's not a secret anymore!