Java中的数组长度属性 [英] array length property in java

查看:72
本文介绍了Java中的数组长度属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要介意,这只是一个关于数组长度属性的简单问题.作为Java的初学者,我遇到了Constants和final关键字,其描述为:

please don't mind this is just a simple question on array length property. As a beginner in Java I came across Constants and final keyword, which is described as:

常量是不可修改的变量,用关键字final声明.它们的值在程序执行期间不能更改.另外,必须在声明期间初始化常量.例如:

Constants are non-modifiable variables, declared with keyword final. Their values cannot be changed during program execution. Also, constants must be initialized during declaration. For examples:

final double PI = 3.1415926;  // Need to initialize

我已经阅读了几乎所有相关文章,但是我对其初始化感到困惑.我尝试使用Netbeans IDE进入其类,但是在那里看不到它的实现.

I have read nearly all the related posts, but I have a confusion about its initialization. I've tried to dive into its class using Netbeans IDE but it's implementation was not visible there.

要获取数组长度的length字段呢?
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

What about the length field to get the length of an array?
http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

如何以及何时初始化(运行时/编译时)?

How and when it is initialized (runtime/compiletime)?

推荐答案

来自

Java虚拟机阵列也是对象.使用一组不同的指令来创建和操作数组. newarray 指令用于创建数字类型的数组.代码:

Java Virtual Machine arrays are also objects. Arrays are created and manipulated using a distinct set of instructions. The newarray instruction is used to create an array of a numeric type. The code:

   void createBuffer() {
        int buffer[];
        int bufsz = 100;
        int value = 12;
        buffer = new int[bufsz];
        buffer[10] = value;
        value = buffer[11];
    }

可能会编译为:

   Method void createBuffer()
    0   bipush 100     // Push int constant 100 (bufsz)
    2   istore_2       // Store bufsz in local variable 2
    3   bipush 12      // Push int constant 12 (value)
    5   istore_3       // Store value in local variable 3
    6   iload_2        // Push bufsz...
    //line below is what you're looking for [comment is mine]
    7   newarray int   // ...and create new int array of that length 
    9   astore_1       // Store new array in buffer
    10  aload_1        // Push buffer
    11  bipush 10      // Push int constant 10
    13  iload_3        // Push value
    14  iastore        // Store value at buffer[10]
    15  aload_1        // Push buffer
    16  bipush 11      // Push int constant 11
    18  iaload         // Push value at buffer[11]...
    19  istore_3       // ...and store it in value
    20  return

newarray int 指令初始化数组及其长度.这意味着,在在运行时初始化数组时,将初始化数组长度.

The newarray int instruction initializes the array and its length. Which means that the array length is initialized when you initialize the array, at runtime.

上面的链接中的说明还说明了 anewarray 指令如何创建引用数组,并显示了类似的模式.

The explanation from link above also explains how an array of references is created by the anewarray instruction, and shows a similar pattern.

这篇关于Java中的数组长度属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆