本文介绍了数组初始化差异java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下两种数组初始化方法有什么区别:
What is the difference between the two following methods of array initialization:
-
Object [] oArr = new Object [] {new Object(),new Object()};
-
Object [] oArr = {new Object(), new Object()};
Object[] oArr = new Object[] {new Object(), new Object()};
Object[] oArr = {new Object(), new Object()};
它与堆/堆栈分配有关吗?
Is it related to heap/stack allocation?
谢谢!
推荐答案
根本没有 - 它们只是表达方式的不同方式同样的事情。
None at all - they're just different ways of expressing the same thing.
然而,第二种形式仅在变量声明中可用。例如,你不能写:
The second form is only available in a variable declaration, however. For example, you cannot write:
foo.someMethod({x, y});
但你可以写:
foo.someMethod(new SomeType[] { x, y });
Java语言规范的相关部分是:
The relevant bit of the Java language specification is section 10.6 - Array Initializers:
这篇关于数组初始化差异java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!