aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "concept in main()" Watch "concept in main()" New topic
Author

concept in main()

Soumya Padhiary
Greenhorn

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:
class{
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: 2902
    
    1

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
Greenhorn

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: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Start reading about varargs here. You are allowed [] or ... in the main method, as it says here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: concept in main()
 
Similar Threads
Help me please
problems running code
Head First Exercise Pg 43
What is the main usage of "String[] args" in public static void main(String[] args)?
Valid signatures for main