• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

concept in main()

 
Soumya Padhiary
Greenhorn
Posts: 20
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its called varargs, equivalent to Java array, added in Java 5.
 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start reading about varargs here. You are allowed [] or ... in the main method, as it says here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic