3

What does this syntax do, with square brackets around the number?

new Integer[0];

I've found it in a codebase I'm maintaining but I can't find any documentation on it. It is used like this:

Set<Form> forms = getForms();
List<Form> formsList = Arrays.asList(forms.toArray(new Form[0]))
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
Sahand
  • 6,046
  • 14
  • 51
  • 105
  • 2
    Possible duplicate of [How to create a generic array in Java?](https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Simulant Jul 01 '19 at 13:59
  • Related: https://stackoverflow.com/questions/1665834/how-can-i-initialize-a-string-array-with-length-0-in-java#1665899 – Druckles Jul 01 '19 at 13:59
  • 1
    Unless you absolutely want a fixed-size mutable list, this is overly complicated. For most uses prefer `List
    formsList = new ArrayList(forms);`.
    – Ole V.V. Jul 01 '19 at 14:03
  • 4
    it is creating an empty `Integer` array, that is, an array that holds `Integer` instances with size 0, that is, actually it can hold no instance of `Integer`- often used as kind of a template, or as return from some method to indicate, for example, that nothing was found – user85421 Jul 01 '19 at 14:04
  • Possible duplicate of [How to convert Set to String[\]?](https://stackoverflow.com/questions/5982447/how-to-convert-setstring-to-string). It’s not the exact same question, but I believe that the answer to this question is there. Or, depending on how you look at it, possible duplicate of [How can I initialize a String array with length 0 in Java?](https://stackoverflow.com/questions/1665834/how-can-i-initialize-a-string-array-with-length-0-in-java) – Ole V.V. Jul 01 '19 at 14:05
  • 2
    In Java generic types are erased by compiler so `toArray` wouldn't be able to create from `Set
    ` proper `Form[]` array because there is no information about `Form` type at runtime. Because of that you need to provide type of array as argument of `toArray`, otherwise it would return `Object[]` array.
    – Pshemo Jul 01 '19 at 14:09

2 Answers2

2

It allocates an array with length zero; e.g. new Integer[0] creates a zero length array of Integer objects.

Why would you do that?

Well look at the javadocs for the form.toArray(T[]) method. Assuming that form is some subtype of Collection they are here.

The purpose of the toArray method is to copy the elements of the target collection (e.g. your form) into an array:

  • If the argument array is large enough to hold all elements, they are copied into that array. The result will be the argument array.

  • If the argument array is too small, a new array is allocated with the same type as the argument array and a length that is (just) enough to hold the elements. The elements are then copied into the new array, and it is returned as the result.

So what the code is actually doing is copying the elements of form to an Integer[] of the right size, and then wrapping the array to give a (fixed sized) List<Integer>. This can then be passed to some other code without worrying that that code might alter the original form collection.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
0

Unlike traditional array which store values like string, integer, Boolean, etc. array of objects stores objects. The array elements store the location of reference variables of the object

Syntax: Class obj[]= new Class[array_length]

  • 1
    Thank you for wanting to contribute. It’s not entirely correct, and it’s not perfectly clear how it answers the question. In Java a string is also an object. And the recommended syntax puts the square brackets right after the class name, `Class[] arr = new Class[array_length];`. – Ole V.V. Jul 01 '19 at 21:40