Java虚拟机结构--数据类型

来源:互联网 发布:qq管家软件 编辑:程序博客网 时间:2024/05/14 23:42

最近在甲骨文官网上看到一篇不错的文章:Java虚拟机规范。全英文的,虽然是英文,但是写的非常不错,因此为了锻炼英语和对java有更深入的理解,现在开始对这篇文章进行学习和翻译,这里采用原文加译文的方式来记录,并且只是节选其中部分段落。
原文网址:http://docs.oracle.com/javase/specs/jvms/se8/html/

我写这一篇博客Java虚拟机结构是上文中的第二章,也就是The structure of the Java virtual machine.

This document specifies an abstract machine. It does not describe any particular implementation of the Java Virtual Machine.

To implement the Java Virtual Machine correctly, you need only be able to read the class file format and correctly perform the operations specified therein. Implementation details that are not part of the Java Virtual Machine’s specification would unnecessarily constrain the creativity of implementors. For example, the memory layout of run-time data areas, the garbage-collection algorithm used, and any internal optimization of the Java Virtual Machine instructions (for example, translating them into machine code) are left to the discretion of the implementor.

译:这篇文档主要描述了一个抽象的虚拟机,并不描述java虚拟机的任何特定实现。
如果需要正确的实现java虚拟机,仅仅需要阅读class file format这一章的内容并正确实现文中所描述的操作即可。
实现细节并不是java虚拟机规范的一部分,因为我们不想限制开发人员的创造性。比如说:运行时数据池的内存结构,使用的垃圾收集算法,以及任何虚拟机内部优化指令。

解读:这一部分内容是本章的摘要部分,主要说明了本章的目的是描述规范,不包含任何实现及其原因。且也说明了class 文件结构的重要性,对于jvm来说。

2.1. The class File Format
Compiled code to be executed by the Java Virtual Machine is represented using a hardware- and operating system-independent binary format, typically (but not necessarily) stored in a file, known as the class file format. The class file format precisely defines the representation of a class or interface, including details such as byte ordering that might be taken for granted in a platform-specific object file format.

译:被java虚拟机执行的代码使用了一个硬件和操作系统独立的二进制文件表示,一般会被存放在一个文件中,这就是我们所熟知的class 文件。class文件结构准确的定义了一个类,接口的行为,还包括了一些诸如字节排序等细节。

解读:这里说明了class 文件结构的作用。class文件的细节在后面章节才会提到。

2.2. Data Types
Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types. There are, correspondingly, two kinds of values that can be stored in variables, passed as arguments, returned by methods, and operated upon: primitive values and reference values.

The Java Virtual Machine expects that nearly all type checking is done prior to run time, typically by a compiler, and does not have to be done by the Java Virtual Machine itself. Values of primitive types need not be tagged or otherwise be inspectable to determine their types at run time, or to be distinguished from values of reference types. Instead, the instruction set of the Java Virtual Machine distinguishes its operand types using instructions intended to operate on values of specific types. For instance, iadd, ladd, fadd, and dadd are all Java Virtual Machine instructions that add two numeric values and produce numeric results, but each is specialized for its operand type: int, long, float, and double, respectively. For a summary of type support in the Java Virtual Machine instruction set, see §2.11.1.

The Java Virtual Machine contains explicit support for objects. An object is either a dynamically allocated class instance or an array. A reference to an object is considered to have Java Virtual Machine type reference. Values of type reference can be thought of as pointers to objects. More than one reference to an object may exist. Objects are always operated on, passed, and tested via values of type reference.

译:Java虚拟机可以操作两种类型的数据:原始类型和引用类型,因此对应的,有两种类型的值可以存放在变量中,用于参数传递,或者被当作返回值,
虚拟机规范希望的是在代码运行期之前就通过编译器来完成类型检查,并且不通过虚拟机本身来完成。原始类型的数据可以在运行期间来确定类型,虚拟机的指令集可以通过指令来完成这一功能。
Java虚拟机可以明确的支持对象类型,这个对象类型可以是动态分配的对象或者是数组,对象的引用可以被认为是虚拟机的类型引用,引用值可以被认为是对象的指针。一个对象具有多个引用也是允许存在的,对象的操作,传递等都可以通过类型引用的值来进行。

解读:这里说明了java的两种数据类型以及类型的检查,对于原始类型的检查,比如int,float,long等可以在运行期间来检查,这一点下面有例子来说明。对于对象类型的操作,都是通过对象的引用来实现,这里可以把引用当作指针来看待。

2.3. Primitive Types and Values
The primitive data types supported by the Java Virtual Machine are the numeric types, the boolean type (§2.3.4), and the returnAddress type (§2.3.3).

The numeric types consist of the integral types (§2.3.1) and the floating-point types (§2.3.2).

The integral types are:

byte, whose values are 8-bit signed two’s-complement integers, and whose default value is zero

short, whose values are 16-bit signed two’s-complement integers, and whose default value is zero

int, whose values are 32-bit signed two’s-complement integers, and whose default value is zero

long, whose values are 64-bit signed two’s-complement integers, and whose default value is zero

char, whose values are 16-bit unsigned integers representing Unicode code points in the Basic Multilingual Plane, encoded with UTF-16, and whose default value is the null code point (‘\u0000’)

The floating-point types are:

float, whose values are elements of the float value set or, where supported, the float-extended-exponent value set, and whose default value is positive zero

double, whose values are elements of the double value set or, where supported, the double-extended-exponent value set, and whose default value is positive zero

The values of the boolean type encode the truth values true and false, and the default value is false.

The First Edition of The Java® Virtual Machine Specification did not consider boolean to be a Java Virtual Machine type. However, boolean values do have limited support in the Java Virtual Machine. The Second Edition of The Java® Virtual Machine Specification clarified the issue by treating boolean as a type.

The values of the returnAddress type are pointers to the opcodes of Java Virtual Machine instructions. Of the primitive types, only the returnAddress type is not directly associated with a Java programming language type.

解读:这里翻译就不用了,单词都挺简单的,直接说结论。
Java虚拟机支持的原始类型是数字类型,布尔类型,以及返回地址类型。规范并没有支持布尔类型,且这也是java语言一个被官方声明的bug了。其他类型都是使用二进制补码形式来标识的。two’s-complement 这里翻译做补码。

2.3.1. Integral Types and Values
2.3.2. Floating-Point Types, Value Sets, and Values

解读:这两节主要说明整数类型的范围,以及浮点数所遵循的IEEE 754 工业标准。

2.3.3. The returnAddress Type and Values
The returnAddress type is used by the Java Virtual Machine’s jsr, ret, and jsr_w instructions (§jsr, §ret, §jsr_w). The values of the returnAddress type are pointers to the opcodes of Java Virtual Machine instructions. Unlike the numeric primitive types, the returnAddress type does not correspond to any Java programming language type and cannot be modified by the running program.

译:返回地址类型通过虚拟机指令:jsr, ret, jsr_w 来操作,返回值指向了虚拟机的操作码,但是返回类型和原始类型不同,是不能通过我们的代码来进行修改。

解读:这里主要说明了返回地址类型的不同点。

2.3.4. The boolean Type
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.

The Java Virtual Machine does directly support boolean arrays. Its newarray instruction (§newarray) enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore (§baload, §bastore).

In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

译:尽管规范定义了布尔类型,但是对该类型的支持非常有限。而且没有专门为该类型提供虚拟机指令,该类型的操作是被编译为整型来处理的。

2.4. Reference Types and Values
There are three kinds of reference types: class types, array types, and interface types. Their values are references to dynamically created class instances, arrays, or class instances or arrays that implement interfaces, respectively.

An array type consists of a component type with a single dimension (whose length is not given by the type). The component type of an array type may itself be an array type. If, starting from any array type, one considers its component type, and then (if that is also an array type) the component type of that type, and so on, eventually one must reach a component type that is not an array type; this is called the element type of the array type. The element type of an array type is necessarily either a primitive type, or a class type, or an interface type.

A reference value may also be the special null reference, a reference to no object, which will be denoted here by null. The null reference initially has no run-time type, but may be cast to any type. The default value of a reference type is null.

This specification does not mandate a concrete value encoding null.

译:规范定义了三种引用类型,对象类型,数组类型和接口类型。它们的值指向了动态创建的对象实例,数组或者是实现该接口的对象实例或数组。
数组类型是一个一维的组合类型,它的长度和类型没有直接关系。这个数组的组合类型可能还是一个数组类型。
一个引用值有可能是一个特殊的null引用,这个引用不指向任何对象,所以这里被声明为null类型。null引用初始化的时候没有任何类型,但是可以转换为其他类型。另外,引用类型的默认值都是null。

解读:这里说明了三种引用类型,并说明这些类型值指向了对应的实例。此外,这里说明了数组类型,如果数组类型的元素是另外一个数组,那么就是我们所说的多维数组了
最后关于null的说明,这里有一道互联网公司的面试题,是这样的:

public class Main {    public static void main(String[] args) {        ((Main)null).print();    }    public static void print(){        System.out.println("HelloWorld");    }}

这里问,如上这个类是否有编译错误,是否可以正确执行。所以,看到这里就非常明白了,null是可以转换的,因为这里可以执行。不过有编译提醒,因为print是static的,所以会提醒print方法可以通过static的方式来访问。

0 0
原创粉丝点击