Search code examples
javacontext-free-grammarbnf

What do square brackets in Java method declarations mean?


The grammar for method declarations in Java is something like the following:

Java method declaration BNF:

method_declaration 
    ::= 
    { modifier } type identifier 
    "(" [ parameter_list ] ")" { "[" "]" } 
    ( statement_block | ";" ) 

And I am wondering what do the square brackets mean.

  1. Can anyone give me an example?
  2. Is method declarations in Java looks like above (What about generics)?
  3. Where can I find complete and actual BNF grammar for Java?

Solution

  • The square brackets are to indicate the method returns an array. For example, you can write a method that returns an array of int as:

    int method()[] { … }
    

    Many people aren't familiar with this syntax though, and it's best avoided.

    You'll find the complete syntax for java 7 here: http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html