java - How did JVM implement array's class? -
can override methods of array? example tostring() or other methods.
import java.lang.reflect.method; public class arraysclasstest { static int[] array = { 1, 2, 3, 1 }; public static void main(string[] args) { class<? extends int[]> class1 = array.getclass(); try { method method = class1.getmethod("tostring"); } catch (nosuchmethodexception | securityexception e) { // todo auto-generated catch block e.printstacktrace(); } }
}
you can't change features of arrays. jls §10.7 array members specifies every member of array:
the members of array type of following:
the
public
final
fieldlength
, contains number of components of array.length
may positive or zero.the
public
methodclone
, overrides method of same name in classobject
, throws no checked exceptions. return type ofclone
method of array typet[]
t[]
.a clone of multidimensional array shallow, creates single new array. subarrays shared.
all members inherited class
object
; method ofobject
not inheritedclone
method.
the specification doesn't allow way of customizing implementation. array's tostring()
method, example, basic 1 inherited object
.
to create array object compiler emits 1 of 3 instructions compiled java bytecode: newarray
primitives, anewarray
reference types, or multinewarray
multidimensional arrays. in implementing instructions, virtual machine creates each array class needed @ runtime (jvms §5.3.3 creating array classes). vm defines dedicated bytecode instructions compiler use getting , setting elements of arrays , getting array's length.
how arrays implemented within vm not specified whatsoever. purely implementation detail, , java compiler doesn't know, or care. actual code involved depends on flavor of virtual machine you're running program on, version of vm, os , cpu it's running on, , relevant runtime options vm configured (e.g., whether in interpreted mode or not).
a quick on openjdk 8 source code turns of relevant machinery arrays:
src/share/vm/interpreter/bytecodeinterpreter.cpp – implements bytecode instructions interpreter, including instructions creating , accessing arrays. it's tortuous , intricate, however.
src/share/vm/c1/c1_rangecheckelimination.cpp – performs clever array bounds check eliminations when compiling bytecode native code.
as arrays core feature of language , vm, it's impossible point 1 source file , "here, class array
code". arrays special, , machinery implements them literally on place.
if want customize behavior of array, thing can not use array directly, use, subclass, or write, collection class internally contains array. gives complete freedom define class's behavior , performance characteristics. however, impossible make custom class be array in java language sense. means can't make implement []
operator or passable method expects array.
Comments
Post a Comment