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 field length, contains number of components of array. length may positive or zero.

  • the public method clone, overrides method of same name in class object , throws no checked exceptions. return type of clone method of array type t[] t[].

    a clone of multidimensional array shallow, creates single new array. subarrays shared.

  • all members inherited class object; method of object not inherited clone 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:

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

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -