0

Can anyone tell me why in Java Array is using square brackets, i.e.

int[] intArray = new int[20];

while for ArrayList they use angle brackets:

ArrayList<Integer> intArrayList = new ArrayList<Integer>();

and what is the difference between the two brackets?

erni
  • 57
  • 7
  • 5
    They denote completely different things. One is an array declaration and the other is a generic type and at some point it was decided that what you see above is the syntax to use for those thing. You might as well ask us why some sentences end with full stops and other with question marks. – OH GOD SPIDERS Feb 04 '21 at 17:37
  • Thanks for the fast feedback. I think I got confused because I expected that brackets in both places denote some kind of "list" or "set" of objects. But apparently this is just true for square brackets, while it is **not** for angle brackets. Right? – erni Feb 04 '21 at 17:46
  • the <> do not themselves denote a list or a set, although a List is a common example. Consider a java tutorial example, a Box: https://docs.oracle.com/javase/tutorial/java/generics/types.html - so the Box is not a list or a set, but it is a generic class, having a generic type denoted by angle brackets. – eis Feb 04 '21 at 18:10

1 Answers1

1

Can anyone tell me why in Java Array is using square brackets?

Because JLS defines such a syntax, for creating arrays:

An array type is written as the name of an element type followed by some number of empty pairs of square brackets []. The number of bracket pairs indicates the depth of array nesting.


while for ArrayList they use angle brackets?

Because, ArrayList<T> is a Java class, which has a constructor, and braces a.k.a round brackets are used to invoke a constructor and create an object of a particular class.

P. S. These two are completely different concepts.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • Ok, sorry for the noob question. I got confused because I thought that any kind of brackets always denotes some kind of "list". But apparently this just holds true for square brackets, while it does not for angle brackets. Right? – erni Feb 04 '21 at 17:48
  • "*any kind of brackets always denotes some kind of "list*" - no; `()` is used in several scenarios.. in the method invocation (constructor is a special method in Java), method signature definition, lambda expression definition.. etc.; `[]` is used for declaring/defining array types; `{}` is used for many different reasons, including block definitions, class definition, method definition, loop constructs, flow-control constructs, etc. – Giorgi Tsiklauri Feb 04 '21 at 17:50
  • "angle brackets are used to invoke a constructor and create an object of a particular class" so are you saying expression `ArrayList intArrayList = new ArrayList();` invokes a constructor twice and creates an object twice, since angle brackets are used in two different places? – eis Feb 04 '21 at 17:55
  • in the quoted part that is clearly what you are saying :) – eis Feb 04 '21 at 17:59
  • that was verbatim quote from your answer. – eis Feb 04 '21 at 18:01
  • I think it is you who should read your own writing a bit more carefully. But I see you've changed it now. :) – eis Feb 04 '21 at 18:05
  • @eis was a sole typo. – Giorgi Tsiklauri Feb 04 '21 at 18:06