java 数组 源码_普通数组的源码是怎么实现的?

取决于JVM实现方式 一般来说和普通对象的存放方式是差不多的 简单的说区别在于数组用了普通对象个数的倍数的连续空间

下面来自JVM规范:

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];

}

might be compiled to:

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...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值