Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Syntax of main

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Syntax of main

    I'm new to Java programming and noticed that in my reading (learning) that there appear to be 2 acceptable syntax for the 'main' statement.

    Example 1:
    class MyClass{
    public static void main(String args[]) {
    // code goes here
    }
    }

    Example 2:
    class MyClass{
    public static void main(String [] args) {
    // code goes here
    }
    }

    note the positon of the [] in the two examples.

    Are both forms acceptable, or does it compile and run correctly only because I am not passing any arguments to the main routine?

    Thanks in advance.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Syntax of main

    (String[] args)
    (String args[])
    (String... helicopter)

    These are all valid declarations, but String[] args is considered best practice when declaring arrays of any sort, as it is far more explicit.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    RKM904 (September 7th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Syntax of main

    The best way to know for sure is to compile and test the program with and without arguments.
    If you get unexpected results, post the console contents and ask some questions.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    RKM904 (September 7th, 2012)

Similar Threads

  1. Syntax in method
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: September 23rd, 2011, 03:01 PM
  2. syntax question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2011, 04:01 PM
  3. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  4. How do you read this syntax?
    By meowCat in forum Java Theory & Questions
    Replies: 5
    Last Post: August 8th, 2010, 03:09 PM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM