Chapter 15 objects and arrays

Only basic types of object references and may be present in the form of Java local variables in the top of stack. Java stack can not accommodate objects.

Java virtual machine objects and basic types of structural separation in the java programming language reflected: objects can not be declared as a local variable, only object references and primitives can. After declaring the object reference does not point to anything meaningful, and only after being explicitly initialized references (whether the object reference points to an existing or create a new object) object reference will point to a real object.

In the Java virtual machine instruction set, in addition to the array, all objects are used to instantiate the same operation code and access. As mentioned earlier, Java in the array is complete (ftill-fledged) objects. Java and other objects, arrays are created dynamically, array references can be used anywhere need to use references to identify the object, any object in the array method can be called. But the Java virtual machine is still using a special code to handle byte arrays.

As with other objects, arrays can not be used as local variables, only the array references can. Object itself generally comprises an array of primitive array or array of object references. If you declare an array of objects, obtained will be an array of object references. Object itself must be explicitly created by the new operator, and Wu to the array members.

15.2 for the operation of the object code

Instantiating a new object needs to be achieved by the new operation code, as shown in Table 15-1. With operation code immediately behind the new word length operand, the word length index operands together represent a length of 16 bits unsigned constant pool. Constant pool of the population at a specific offset information for the new location is given the object's class. If you do not have this information, the virtual opportunity to resolve the constant pool of the population. It will be established as a new object in the heap this instance, the default initial value to initialize the object instance variables, then the pressure on people refer to the new object stack.

The pop-up object field, pressure people opcode stack as shown in Table 15-2. putfield and getfield only two opcode field is only performed in the case of instance variables. putstatic getstatic static and variable access operation (which will be discussed later). putfield and getfield instructions have two operands, both operands together represent 16-bit length index unsigned constant pool. This index points to entry contains a constant pool of the class Yu segment belongs, name and type of information. If you do not have this information, virtual opportunity to resolve the constant pool of the population. putfield and getfield reference to an object stack operations. putfield instruction fetch instance variable values ​​from the stack, getfield instruction value of the instance variable pressure obtained human stack.

Object operation code to be described below is used to check the reference is to the top of the stack - for instance of a class, or pointing to the operand following the operation code as an index of the interface. In both cases, the virtual machine will have a constant pool of the population directed unsigned 16-bit length of the index, then its value is assigned to two bytes after the opcode. If you do not have these contents, the virtual parsing constant pool entry.

If the given object is not an instance of the specified class or interface, checkcast instruction CheckCast-Exception is thrown an exception. Otherwise, nothing will happen. Object reference is still the stack, then the next instruction will be executed. This instruction is to ensure the safety run-time type conversion, and is part of the Java Virtual Machine security framework.

As shown in Table 15-4, instanceof instruction pops the top of the stack from the object reference, or 0 and 1 person pressure. If the object is indeed a specified class or instance of an interface, it is pushed into the stack 1; otherwise, it is pressed into the stack to 0. instanceof instanceof instructions used to implement java language keywords, the keyword is used to test whether an object is a specific instance of the class or interface.


15.3 arrays for the opcode
as shown in Table 15-6, instantiating a new array of work can be done by newarray, anewarray and multianewarray opcode. newarray operation code used to create the array of basic type, instead of an array of object references. Followed by the specified number of basic types of single-byte opcode operation newarray "atype". newarray instruction to create byte, short, char, int, long, float, double , or an array of boolean type.

It should be noted that, when boolean, Java virtual machine instruction array will be created to operate the unit when the bit array type explicitly declared. This feature enables to achieve, especially when the need to control the memory requirements can be used to compress the bit map mode boolean array. In this representation, each element of a boolean array can be used to represent a. When the memory is not very stressful time, boolean arrays can use byte arrays to achieve, even though it will consume more memory. Whether the virtual machine for which the use of an internal array of boolean implemented, will be used to access an opcode byte access array element boolean array element. These opcodes will be discussed later in this chapter.

anewarray instruction used to create an array of object references. anewarray instruction has two single-byte operand opcodes anewarray thereof immediately after, both operands together represents an index of a length of 16 bits unsigned constant pool. An array of objects for which you created, describing their respective classes can be found in the constant pool by the index. If you do not have a description, the virtual machine will resolve the constant pool entrance. This instruction is the array of object references allocate space, and the reference value is initialized to null.

multianewarray instruction for dispensing multi-dimensional array, a so-called multi-dimensional array, is an array of arrays. Multi-dimensional arrays may be performed by repeating dispensing and using anewarray newarray. multianewarray instruction is just trying to create multidimensional arrays require compressed into a bytecode instruction. multianewarray instruction has two single-byte immediately following the opcode multianewarray operands, both operands together represent 16-bit length index unsigned constant pool. An array of objects for which you created, describing their respective classes can be found in the constant pool by the index. If you do not have a description, the virtual machine will resolve the symbolic references. Immediately after two operands, the operation is a single-byte unsigned, the operand is used to indicate the dimension of a multidimensional array. The length of each dimension will pop from the stack. This instruction for all assign an array of space consisting of a multidimensional array.

multianewarray instructions for use included CONSTANT_Class population constant pool entry with an array of class names. For example, a four-dimensional type float constant pool entry array will have a form such as "[[[[F" name Yu. Constant pool entry class name can have a specified number of bytes more than the dimension (but not less) left parenthesis. Virtual machines typically create dimension array dimension according to the number of bytes.

As shown in Table 15-7 arraylength instruction. arraylength pops up a reference array from the top of the stack, and the length of the array stack pressure on people.

Opcode As shown in Table 15-8 acquires element from an array Che. VM pop from the stack array and the array reference index, then a value onto the stack located in the specified index of the given array. baload opcode byte, or the value of the boolean sign extended to an int, then the int type values ​​onto the stack. Similarly, saload the opcode value of type short sign extended to an int and then this value onto the stack of type int person. caload the type char by zero-extended converted to an int type, then this value is then pressed person int type stack.

initAnArray () method is only a three-dimensional array of allocating and initializing operations. The simulation demonstrates the process Java virtual machine processing three-dimensional array. In order to request allocation of three-dimensional array of instruction multianewarray respond, Java virtual machine to create a one-dimensional array of a tree. Multianewarray returned by instruction basis of references to a one-dimensional array initAnArray () method, an array of five basic elements: threeD [0] to threeD [4]. Each element of the underlying array itself is another one-dimensional array containing four reference elements, these two arrays can threeD [0] [0] [0] to threeD [4] [3] [2] to access. This 20 array elements are also an array of references, each of which contains three elements. These elements are of type int, while they are also the three-dimensional array of elements, all of which can threeD [0] [0] [0] to threeD [4] [3] [2] to access.

In order to respond to initAnArray () method multianewarray instructions, Java virtual machine creates an array of array elements have five, five have four array elements of the array 20 has an array of three elements of type int. Java virtual machine for this 26 array allocated on the heap space, initializing their elements (so that all the elements form a tree), and then returns a reference to the underlying array.

In order to give a three-dimensional array of elements of this assignment of an int type, Java Virtual Machine use aaload to get a basic element of the array, and then the java virtual machine again this element (which itself is an array of arrays) to get the branch instruction using aaload a sub-element array, the sub-element is a pointer to level 3 int type array reference. Finally, Java Virtual Machine use iastore command to a value of type int Wu elements of this series to the third group. Java Virtual Machine through access to multiple one-dimensional array to complete the operation of the multi-dimensional arrays.

Guess you like

Origin www.cnblogs.com/mongotea/p/11980079.html