举个例子,如下:

public void t() {
	String[][] a = new String[][] {
		                 { "x", "y" }
		             };	

	String[] b = new String[10];
	String[] c = new String[] { "x", "y" };
	String[] d = { "x", "y" };
}

其中a如下语句生成的class内容一样,如下:

String[][] a2 =  { { "x", "y" } };

当在创建时初始化数组时,不能指定数组的大小,如下会报错:

String[] c = new String[2] { "x", "y" };
String[][] a1 = new String[2][] { { "x", "y" } };

Eclipse编译器报错:Cannot define dimension expressions when an array initializer is provided  

则生成的class内容如下:

public void t();
 flags: ACC_PUBLIC
 Code:
  stack=7, locals=5, args_size=1
     // 创建第一个数组

     0: iconst_1      // 创建的一维数组的长度,这是因为初始化模块中只有一个元素,即{"x","y"}
     1: anewarray     #2  // class "[Ljava/lang/String;"
     4: dup

     5: iconst_0      // 表示二维数组的第0个索引上要放入一维数组值
     6: iconst_2      // 由于一维数组{"x","y"}的长度为2,所以加载常量2
     7: anewarray     #3   // class java/lang/String

    10: dup
    11: iconst_0      // 在一维数组索引0的位置存储x
    12: ldc           #4   // String x
    14: aastore

    15: dup
    16: iconst_1      // 在一维数组索引1的位置存储y
    17: ldc           #5  // String y
    19: aastore

    20: aastore
    21: astore_1

    // 创建第二个数组
    22: bipush        10
    24: anewarray     #3  // class java/lang/String
    27: astore_2

    // 创建第三个数组
    28: iconst_2
    29: anewarray     #3  // class java/lang/String

    32: dup
    33: iconst_0
    34: ldc           #4  // String x
    36: aastore

    37: dup
    38: iconst_1
    39: ldc           #5  // String y
    41: aastore

    42: astore_3

    // 创建第四个数组
    43: iconst_2
    44: anewarray     #3  // class java/lang/String
    47: dup
    48: iconst_0
    49: ldc           #4  // String x
    51: aastore
    52: dup
    53: iconst_1
    54: ldc           #5  // String y
    56: aastore
    57: astore        4
    59: return

首先来详细分析第一个数组的创建过程:

0: iconst_1
 1: anewarray     #2                  // class "[Ljava/lang/String;"

 4: dup
 5: iconst_0
 6: iconst_2
 7: anewarray     #3                  // class java/lang/String

10: dup
11: iconst_0
12: ldc           #4                  // String x
14: aastore

15: dup
16: iconst_1
17: ldc           #5                  // String y
19: aastore

20: aastore
21: astore_1

第一个数组的树形结构如下:

具体执行过程如下:

0: iconst_1
调用ImmediateItem(1)的load方法将常数加载到操作栈中

1: anewarray #2 // class "[Ljava/lang/String;"
将一维数组类型存入到常量池中,直接向操作栈中压入这个数组类型String[]

4: dup
生成一个StackItem(Object)并调用其duplicate()方法

5: iconst_0
调用ImmediateItem(0)的load方法将常数加载到操作栈中

6: iconst_2
调用ImmediateItem(2)的load方法将常数加载到操作栈中

7: anewarray #3 // class java/lang/String
将String类型存入到常量池中,直接向操作栈中压入这个类型String

10: dup
生成一个StackItem(Object)并调用其duplicate()方法

11: iconst_0
调用ImmediateItem(0)的load方法将常数加载到操作栈中

12: ldc #4 // String x
调用ImmediateItem("x")的load方法将常数加载到操作栈中
14: aastore
创建并调用IndexedItem(object)并调用其store()方法

15: dup
16: iconst_1
17: ldc #5 // String y
19: aastore

20: aastore
为如下的执行代码返回StackItem(object)
21: astore_1
创建并调用IndexedItem(object)并调用其store()方法

  

  

 

Gen对于数组Array的处理的更多相关文章

  1. Java ArrayList和Vector、LinkedList与ArrayList、数组(Array)和列表集合(ArrayList)的区别

    ArrayList和Vector的区别ArrayList与Vector主要从二方面来说.  一.同步性:   Vector是线程安全的,也就是说是同步的,而ArrayList是线程序不安全的,不是同步 ...

  2. go 数组(array)、切片(slice)、map、结构体(struct)

    一 数组(array) go语言中的数组是固定长度的.使用前必须指定数组长度. go语言中数组是值类型.如果将数组赋值给另一个数组或者方法中参数使用都是复制一份,方法中使用可以使用指针传递地址. 声明 ...

  3. javascript类型系统——数组array

    × 目录 [1]创建 [2]本质 [3]稀疏[4]长度[5]遍历[6]类数组 前面的话 除了对象之外,数组Array类型可能是javascript中最常用的类型了.而且,javascript中的数组与 ...

  4. swift基本用法-数组array

    数组简单用法 //------------------------------------------------------------------------------ // 1. 数组定义 / ...

  5. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  6. Javascript基础系列之(四)数据类型 (数组 array)

    字符串,数值,布尔值都属于离散值(scalar),如果某个变量是离散的,那么任何时候它只有一个值. 如果想使用变量存储一组值,就需要使用数组(array). 数组是由多个名称相同的树值构成的集合,集合 ...

  7. AS3 - 数组Array的几个常用方法(附样例)

    AS3 - 数组Array的几个常用方法(附样例) 2015-03-30 10:39发布:hangge浏览:241   Flex/Flash开发中,经常会使用到数组,下面总结了一些数组的常用方法. 1 ...

  8. Linux数组array基础

    Linux数组array基础[${a[*]}和$a的区别] Bash中,数组变量的赋值有两种方法: (1) name = (value1 ... valuen) 此时下标从0开始 (2) name[i ...

  9. 学习Swift -- 数组(Array) - 持续更新

    集合类型--数组 Array是Swift中的一种集合类型:数组,数组是使用有序列表储存同一类型的多个值,与OC的NSArray的最大不同是,Swift的数组是值类型,OC的数组是引用类型 声明数组的方 ...

随机推荐

  1. char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

  2. idea常用插件介绍

    常用插件 mybatis mapper 选择plugins,搜索mybatis plugin 激活教程 使用 插件的使用

  3. 51nod 1239 欧拉筛模板

    #include<iostream> #include<cmath> #include<cstring> #include<cstdio> #inclu ...

  4. MySQL查询实例

    单表查询查询所有列 1 SELECT * FROM product; 查询指定列 1 SELECT pro_name,price,pinpai FROM product; 添加常量列 1 SELECT ...

  5. 第74讲:从Spark源码的角度思考Scala中的模式匹配

    今天跟随王老师学习了从源码角度去分析scala中的模式匹配的功能.让我们看看源码中的这一段模式匹配: 从代码中我们可以看到,case RegisterWorker(id,workerHost,.... ...

  6. Hbase_shell操作

    创建表 create 'user_action_table', 'action_log', 'action'-- 执行结果=> Hbase::Table - m_table 描述表信息 desc ...

  7. 【转】利用线程更新ListView (2014-09-28 08:25:20)

    http://blog.sina.com.cn/s/blog_44fa172f0102v2x0.html procedure TForm5.Button3Click(Sender: TObject); ...

  8. [译]C#和.NET中的字符串

    原文地址:Jon Skeet:Strings in C# and .NET System.String 类型(在C#语言中对应的别名是string)是.NET最重要的类型之一,不幸的是在它身上存在了太 ...

  9. CentOS 7通过Firewall开放防火墙端口

    发现在CentOS 7上开放端口用iptables没效果(或者是sodino没找到正确的命令,传说Centos7 下默认的防火墙是 Firewall,替代了之前的 iptables)… 使用firew ...

  10. NHibernate问题求大神解决!!!

    这是我定义的实体类 对应的数据库表 映射文件 数据访问层写的是插入语句 错误: 捕捉到 NHibernate.Exceptions.GenericADOException HResult=-21462 ...