Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[L array notation - where does it come from?

Tags:

java

arrays

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?

like image 219
Michael Berry Avatar asked Feb 23 '11 00:02

Michael Berry


People also ask

What is Ljava Lang string?

Ljava.lang.String stands for the string class (L followed by class/interface name) Few Examples: Class.

How do you use array length in Java?

To get the size of a Java array, you use the length property. To get the size of an ArrayList, you use the size() method. Know the difference between the Java array length and the String's length() method.


1 Answers

[ 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 ):

enter image description here

Reference: Field description on the JVM specification

like image 171
OscarRyz Avatar answered Oct 23 '22 20:10

OscarRyz