I was creating String array with few elements. At that time I notice that
I have put comma after first element and it’s still compile. I thought it won’t compile. Then I print the size and it says 1
String args [] = {request.getParentMessageID() , };
System.out.println(args.length);
So
String args [] = {request.getParentMessageID() , }; and String args [] = {request.getParentMessageID()};
both behave as same.
Could someone kindly explain why these are not different and Why it compile.
Trailing commain a array initialization like that areignored by compiler. Those are generally added so that later on adding something to the array, just requires adding the element without worrying about comma.So it is allowed and is a valid syntax.
This is also listed in
JLS - Section#10.6 (Array Initializers): –