Results 1 to 4 of 4
  1. #1
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Trailing comma in array initialization - why does it work?

    Hi,

    String[] aas = { "array", "of", "String", };
    int [] ix = {1,3,};


    What's the deal here? It works, but why?
    If you instead try

    int [] ix = {1,,3};

    it doesn't work. What's so special about a trailing comma that makes it acceptable?

    Thanks!
    m

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Trailing comma in array initialization - why does it work?

    Quote Originally Posted by mikomi View Post
    It works, but why?
    Because the specification says so.
    Chapter*10.*Arrays

    And it's better to use the code tags, not monospaced fonts, for posting code.
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Trailing comma in array initialization - why does it work?

    Java borrowed it from C: it is easier for code generators to generate tuples <element> <comma> for each element (no exception for the last element).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Trailing comma in array initialization - why does it work?

    db: thanks
    Jos: thanks for the added explanation; that was what I was getting at. Now I know I will sleep tonight
    Cheers,
    m

Similar Threads

  1. Array of object initialization
    By rybo85 in forum New To Java
    Replies: 8
    Last Post: 05-03-2012, 05:49 AM
  2. Int Array Initialization Problem
    By dragstang86 in forum New To Java
    Replies: 5
    Last Post: 03-28-2012, 09:54 PM
  3. An Array of Objects (Class Initialization)
    By Chris_C in forum New To Java
    Replies: 9
    Last Post: 02-09-2011, 05:49 AM
  4. Array initialization difference
    By sln69 in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2008, 02:38 AM
  5. 2D Array Initialization
    By M77 in forum Advanced Java
    Replies: 3
    Last Post: 06-04-2008, 02:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •