Is there any difference between “Object[] x” and “Object x[]”?

前端 未结 5 1935
时光说笑
时光说笑 2020-12-03 22:17

I was updating a legacy code base in Java and I found a line like this:

Object arg[] = { new Integer(20), new Integer(22) };

That line catc

5条回答
  •  爱一瞬间的悲伤
    2020-12-03 22:57

    Another good reason to write Integer[] ints instead of Integer ints[] is because of inheritance relations: Integer[] is subtype of Number[] is subtype of Object[].

    In other words, you can put Integers in an Object array, so you can think of the [] as part of the object's type definition -- which is why it makes sense to have it close to the type instead of the object name.

提交回复
热议问题