Java中不能在声明数组大小的同时初始化的根本原因是什么?
PHP中文网
PHP中文网 2017-04-17 17:25:34
0
3
664

找了一下发现网上只说就这么定义的,哪位大神能帮忙解释一下根本原因?
是不是能从虚拟机的角度解释一下?
谢谢啦!

eg.

String s[] = new String[] {"1","2","3"};正确
String s[] = {"1","2","3"};正确
String s[] = new String[3] {"Zero","One","Two"};错误
PHP中文网
PHP中文网

认证0级讲师

reply all(3)
阿神

Array initialization is to obtain the fixed capacity of the array. The
{"","",""} method can obtain the fixed capacity of the array and assign a value to each element.

new String[3] can also tell the compiler the fixed capacity of the array, and by the way, set each element to null,

However, the two methods of standardizing the fixed capacity of the array at the same time may cause conflicts in syntax,

For example, new String[4]{"","",""},

So in order to avoid such ambiguity, this way of writing that also regulates the array capacity should be prohibited from a grammatical perspective

Peter_Zhu

The grammar is stipulated in this way. . .
It has nothing to do with virtual machines

https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.10.1

小葫芦

The array applies for a fixed size memory area

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!