[L数组符号 - 它从何而来? [英] [L array notation - where does it come from?

查看:165
本文介绍了[L数组符号 - 它从何而来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常看到消息使用 [L 然后选择一种来表示数组,例如:

[Ljava.lang.Object;不能被转换为[Ljava.lang.String;

(以上是我刚掏出一个任意的例子。)我知道这意味着一个数组,但哪里语法从何而来?为什么一开始 [但没有右方括号?以及为什么L·它是纯粹的任意还是有它背后的一些其他历史/技术原因?


解决方案

[表示阵列中, Lsome.type.Here 表示类型。这是类似于使用的类型描述符的内部的字节$​​ C $ C 看到的&教派; Java虚拟机规范 4.3 - 采摘为尽量简短即可。唯一的区别是,该的真正的描述符使用 / ,而不是用于表示包

例如,对于原语的值: [I 对于int数组,二维数组是: [I

由于类可能有任何名称,这将是难以确定它是什么类,因此,类名结尾的 ;

描述符也被用于重新present的类型的字段和方法

例如:

 (IDLjava /郎/螺纹;)Ljava /郎/对象;

...对应的参数的方法 INT 双击螺纹键,返回类型为对象

修改

您也可以使用java dissambler看到这个.class文件

  C:>更> S.java
S级{
  你好对象(INT I,双D,龙J,线程t){
   返回新的对象();
  }
}
^ C
C:>的javac S.javaC:>的javap -verbose小号
S级扩展java.lang.Object
  的SourceFile:S.java
  次版本:0
  主要版本:50
  常量池:
常量#1 =#方法2#12; //爪哇/郎/对象<&初始化GT;:()V
常量#2 =#类13; //爪哇/郎/对象
常量#3 =#类14; //小号
常量#4 = Asciz<初始化取代;
常量#5 = Asciz()V;
常量#6 = Asciz code;
常量#7 = Asciz LineNumberTable;
常量#8 = Asciz您好;
常量#9 = Asciz(IDJLjava /郎/螺纹;)Ljava /郎/对象;;
常量#10 = Asciz的SourceFile;
常量#11 = Asciz S.java;
常量#12 =#NameAndType 4:5#; //<&初始化GT;:()V
常量#13 = Asciz爪哇/郎/对象;
常量#14 = Asciz S;{
S();
  code:
   堆栈= 1,当地人= 1,Args_size = 1
   0:aload_0
   1:invokespecial#1; //方法的Java /郎/对象<&初始化GT;:()V
   4:回归
  LineNumberTable:
   线路1:0
你好的java.lang.Object(INT,双,长,java.lang.Thread中);
  code:
   堆栈= 2,当地人= 7,Args_size = 5
   0:新#2; //级Java /郎/对象
   3:DUP
   4:invokespecial#1; //方法的Java /郎/对象<&初始化GT;:()V
   7:areturn
  LineNumberTable:
   线路3:0
}

和原始的类文件(请看第5行):

参考: 字段说明/ p>

I've often seen messages that use [L then a type to denote an array, for instance:

[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

(The above being an arbitrary example I just pulled out.) I know this signifies an array, but where does the syntax come from? Why the beginning [ but no closing square bracket? And why the L? Is it purely arbitrary or is there some other historical/technical reason behind it?

解决方案

[ stands for Array, the Lsome.type.Here means the type. That's similar to the type descriptors used internally in the bytecode seen in §4.3 of the Java Virtual Machine Specification -- picked to be as brief as possible. The only difference is in that the real descriptors use / rather than . for denoting packages.

For instance, for primitives the value is: [I for array of int, a two-dimensional array would be: [[I.

Since classes may have any name, it would be harder to identify what class it is, hence the L, the class name ends with a ;

Descriptors are also used to represent the types of fields and methods.

For instance:

(IDLjava/lang/Thread;)Ljava/lang/Object;

... corresponds to a method whose parameters are int, double, and Thread and the return type is Object

edit

You can also see this in .class files using the java dissambler

C:>more > S.java
class S {
  Object  hello(int i, double d, long j, Thread t ) {
   return new Object();
  }
}
^C
C:>javac S.java

C:>javap -verbose S
class S extends java.lang.Object
  SourceFile: "S.java"
  minor version: 0
  major version: 50
  Constant pool:
const #1 = Method       #2.#12; //  java/lang/Object."<init>":()V
const #2 = class        #13;    //  java/lang/Object
const #3 = class        #14;    //  S
const #4 = Asciz        <init>;
const #5 = Asciz        ()V;
const #6 = Asciz        Code;
const #7 = Asciz        LineNumberTable;
const #8 = Asciz        hello;
const #9 = Asciz        (IDJLjava/lang/Thread;)Ljava/lang/Object;;
const #10 = Asciz       SourceFile;
const #11 = Asciz       S.java;
const #12 = NameAndType #4:#5;//  "<init>":()V
const #13 = Asciz       java/lang/Object;
const #14 = Asciz       S;

{
S();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return
  LineNumberTable:
   line 1: 0


java.lang.Object hello(int, double, long, java.lang.Thread);
  Code:
   Stack=2, Locals=7, Args_size=5
   0:   new     #2; //class java/lang/Object
   3:   dup
   4:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   7:   areturn
  LineNumberTable:
   line 3: 0


}

And in raw class file ( look at line 5 ):

Reference: Field description on the JVM specification

这篇关于[L数组符号 - 它从何而来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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