1

I was reading and faced with interesting look of main method which I didn't see before. Is there any difference between

  public static void main(String[] args) 

and

  public static void main(String a[])        

As I see applying either of them gives me the same output

  public static void main(String a[]){
        List <Integer> li = new ArrayList <Integer>();
        ListIterator <Integer> liItr = null;

        li.add(1);
        li.add(2);
        li.add(3);
        li.add(4);
        li.add(5);
        li.add(6);
        li.add(7);

       liItr = li.listIterator();
       System.out.println("Elemnets in forward direction");
       while(liItr.hasNext()){
           System.out.println(liItr.next());
       }
       System.out.println("Elements in backward direction");
       while(liItr.hasPrevious()){
           System.out.println(liItr.previous());
      }
    }
  }

P.S. I consider myself as a beginner of Java. If someone could highlight giving me some explanation on that it would be nice

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

4 Answers4

2

From http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.2

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

For example:

byte[] rowvector...

This declaration is equivalent to:

byte rowvector[]...

BTW, not many realize that unfortunately this rule affects also Type methodSignature pair, not only Type variable. So something like

int giveMeArray()[]{
//  ^^^^^^^^^^^^^ - method signature
        return new int[10];
}

is also valid Java code.

Anyway this way of declaring array can be useful if you would like to create... lets say 2 and 3 dimensional arrays. So instead of

int[][] arr2d;
int[][][] arr3d;

you can just write

int[][] arr2d, arr3d[];
//                  ^^-additional dimension
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • +1 for the interesting but harder-to-read-and-maintain example about array declaration for methods. I feel good for not realizing that, though. – Luiggi Mendoza May 18 '14 at 22:44
1

No, there's no difference. The only difference is the array declaration:

String[] args

vs

String a[]

Both are valid.

From Java Language Specification. Chapter 10. Arrays. 10.2. Array Variables. (emphasis mine):

Example 10.2-1. Declarations of Array Variables

int[]     ai;        // array of int
short[][] as;        // array of array of short
short     s,         // scalar short
          aas[][];   // array of array of short
Object[]  ao,        // array of Object
          otherAo;   // array of Object
Collection<?>[] ca;  // array of Collection of unknown type

The [] may appear as part of the type at the beginning of the declaration, or as part of the declarator for a particular variable, or both.

For example:

byte[] rowvector, colvector, matrix[];

This declaration is equivalent to:

byte rowvector[], colvector[], matrix[][];
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

No there isn't a difference. See also http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html which states this: "You can also place the brackets after the array's name:", but suggests it is bad form.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
jordan
  • 959
  • 7
  • 17
  • 1
    @KickButtowski it is not wrong (in terms of content), but you cannot add that kind of info in other user's answer unless it is community wiki. You can post a comment advising this user to improve the answer and tell how, or write your own answer. – Luiggi Mendoza May 18 '14 at 23:08
0

String[] args is as valid as String args[] and as String a[].

Java has no restriction regarding arguments naming and authorizes both styles of array declaration syntaxes (brackets after or before the variable name) => Matter of taste.

Mik378
  • 21,881
  • 15
  • 82
  • 180
  • See the link I provided above. Java says it is not a matter of taste. – jordan May 18 '14 at 22:45
  • 1
    @jordan Bad != Discouraged. There isn't any difference at all, just a better way for developer to not forget that he deals with an array. So it is clearly a matter of taste. – Mik378 May 18 '14 at 23:03