8
private final String[] okFileExtensions = new String[] { "csv" };

Would someone please explain why {} is written after a String array declaration?

Thanks.

1
  • 1
    Consider using an enumeration, instead of explicitly hardcoding the file extensions. Jan 11, 2010 at 17:01

4 Answers 4

16

It's an array of one element. In this case containing the String "csv".

When written as part of a declaration, this can be written in a more concise form:

private final String[] okFileExtensions = { "csv" };

Multiple-element arrays use commas between values. There needn't be any values at all.

private final String[] okFileExtensions = { "csv", "tsv" };

private final String[] noFileExtensions = { };

It may be worth noting that although the reference is final the array is not. So you can write:

    okFileExtensions[0] = "exe";

A way to get around this is to switch to collections and use an unmodifiable implementation:

private final Set<String> okFileExtensions = Collections.unmodifiableSet(
    new HashSet<String>(Arrays.asList({
        "csv"
    }));

JDK8 is intended to have enhancement to collections that will make this more concise. Probably List and Set literals within the language. Possibly:

private final Set<String> okFileExtensions = { "csv" };

Collections should generally be preferred over arrays (for reference types).

4
  • 2
    If you wanted to add multiple file extension values to the array you would just separate them with commas eg. private final String[] okFileExtensions = { "csv", "txt", "sql" };
    – TabbyCool
    Jan 11, 2010 at 16:02
  • I have now added that to my answer. Jan 11, 2010 at 16:57
  • Collections should generally be preferred over arrays (for reference types) that depends largely on what the purpose and scope of the array is. As given here this is much too general for my taste.
    – rsp
    Jan 11, 2010 at 17:00
  • +1 for mentioning the JDK7 enhancement. Didn't knew that particular one. Looks very useful.
    – BalusC
    Jan 11, 2010 at 17:01
4

That's the Java's valid syntax for array declaration.

You may use that when you are passing an array without declaring a variable:

 public void printArray( String [] someArray ) {
      for( String s : someArray) {
          System.out.println( s );
      }
  }

And invoke it like this:

  printArray( new String [] { "These", "are", "the", "contents"} );

The curly braces can only be used when declaring the array so the following is not allowed:

Stirng [] a;

a = {"on", "two"};
2

Creating an array of strings inline.

1

I think a less verbose (also confusing) declaration would have been :

private final String[] okFileExtensions = {"csv"};
1
  • 1
    Maybe ... but the new <type>[] {...} syntax can do more.
    – Stephen C
    Jan 11, 2010 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.