Initializing array with values - should I explicitly instance the class or not?

后端 未结 3 802
一整个雨季
一整个雨季 2020-12-17 10:28

I often see arrays being initialized like this:

String[] array = new String[] { \"foo\", \"bar\", \"baz\" };

But reading the Language Basic

3条回答
  •  死守一世寂寞
    2020-12-17 11:01

    Is there any difference between these?

    There is no difference in the end result. However, as per the JLS § 10.6, you cannot use the array initializer synax in every context.

    An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.

提交回复
热议问题