`Virtual Machine
`Specification
`
`Tim Lindholm
`Frank Yellin
`
`Az
`vv
`
`ADDISON-WESLEY
`
`An imprint of Addison Wesley Longman, Inc.
`Reading, Massachusetts + Harlow, England * Menlo Park, California
`Berkeley, California » Don Mills, Ontario * Sydney
`Bonn + Amsterdam * Tokyo « Mexico City
`
`Page 1 of 27
`
`GOOGLEEXHIBIT 1025
`
`Page 1 of 27
`
`GOOGLE EXHIBIT 1025
`
`
`
` Copyright © 1997 Sun Microsystems, Inc.
`
`SON,
`WEI
`2550 Garcia Avenue, Mountain View, California 94043-1{00U.S.A.
`All rights reserved.
`
`by
`
`3.4253
`
`Duke™ designed by Joe Palrang.
`
`RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States
`Government is subject to the restrictions set forth in DFARS 252.227-7013 (c)(1)(ii) and
`FAR 52.227-19.
`
`The release described in this manual may be protected by one or more U.S.patents, foreign
`patents, or pending applications.
`
`Sun Microsystems, Inc. (SUN) hereby grants to you a fully-paid, nonexclusive, nontransfer-
`able, perpetual, worldwide limited license (without the right to sublicense) under SUN's
`intellectual property rights that are essential to practice this specification. This license allows
`and is limited to the creation and distribution of clean room implementations of this specifi-
`cation that: (i) include a complete implementationof the current version ofthis specification
`without subsetting or supersetting; (ii) implementall the interfaces and functionality of the
`standard java.* packages as defined by SUN, without subsetting or supersetting; (iii) do
`not add any additional packages, classes or methods to the java. * packages; (iv) pass all
`test suites relating to the most recent published version of this specification that are available
`from SUN six (6) months prior to any beta release of the clean room implementation or
`upgrade thereto; (v) do not derive from SUN source code or binary materials; and (vi) do not
`include any SUN binary materials without an appropriate and separate license from SUN.
`
`Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun
`Microsystems Computer Corporation logo, Java, JavaSoft, JavaScript, and HotJava are
`trademarksor registered trademarks of Sun Microsystems, Inc. UNIX®is a registered trade-
`mark in the United States and other countries, exclusively licensed through X/Open Com-
`pany, Ltd. All other product names mentioned herein are the trademarks of their respective
`owners.
`
`THIS PUBLICATIONIS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND,
`EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
`IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
`PURPOSE, OR NON-INFRINGEMENT.
`
`THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPO-
`GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFOR-
`MATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW
`EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS,
`INC. MAY MAKE
`IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PRO-
`GRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.
`
`Text printed on recycled and acid-free paper
`
`ISBN 0-201-63452-X
`12345678 9-MA-00999897
`First printing, September 1996
`
`Page 2 of 27
`
`Page 2 of 27
`
`
`
`
`
` CHAPTE 1
`
`Introduction
`
`A Bit of History
`
`JAVAis a general-purpose concurrent object-oriented programming language.Its
`syntax is similar to C and C++,butit omits many of the features that make C and
`C++ complex, confusing, and unsafe. Java was initially developed to address the
`problemsof building software for networked consumer devices. It was designed to
`support multiple host architectures and to allow secure delivery of software compo-
`nents. To meet these requirements, compiled Java code had to survive transport
`across networks, operate on any client, and assure the client that it was safe to run.
`The popularization of the World Wide Web made these attributes of Java
`much more interesting. The Internet demonstrated how media-rich content could
`be made accessible in simple ways. Web browsers such as Mosaic enabled mil-
`lions of people to roam the Net and made Websurfing part of popular culture. At
`last there was a medium where what you saw and heard wasessentially the same
`whether you were using a Mac, PC, or UNIX machine, and whether you were
`connected to a high-speed network or a slow modem.
`Web enthusiasts soon discovered that the content supported by the Web’s
`HTML document format was too limited. HTML extensions, such as forms, only
`highlighted those limitations, while making it clear that no browser could include
`all the features users wanted. Extensibility was the answer.
`Sun’s HotJava browser showcases Java’s interesting properties by makingit
`possible to embed Java programs inside HTML pages. These programs, known as
`applets, are transparently downloaded into the HotJava browser along with the
`HTML pagesin which they appear. Before being accepted by the browser,applets
`are carefully checked to make sure they are safe. Like HTML pages, compiled
`Java programs are network- and platform-independent. Applets behave the same
`
`Page 3 of 27
`
`Page 3 of 27
`
`
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`way regardless of where they come from, or what kind of machinethey are being
`loaded into and run on.
`With Java as the extension language, a Web browseris no longerlimited to a
`fixed set of capabilities. Programmers can write an applet once and it will run on
`any machine, anywhere. Visitors to Java-powered Web pages can use content
`found there with confidencethatit will not damage their machine.
`Java has demonstrated a new way to use the Internet to distribute software.
`This new paradigm goes beyond browsers. Wethink it is an innovation with the
`potential to change the course of computing.
`
`The Java Virtual Machine
`
`The Java Virtual Machineis the comerstone of Sun’s Java programming language.It
`is the component of the Java technology responsible for Java’s cross-platform deliv-
`ery, the small size of its compiled code, and Java’s ability to protect users from mali-
`cious programs,
`The Java Virtual Machineis an abstract computing machine. Like a real com-
`puting machine, it has an instruction set and uses various memoryareas.It is rea-
`sonably common to implement a programming languageusing a virtual machine:
`the best-known virtual machine may be the P-Code machine of UCSD Pascal.
`Thefirst prototype implementation of the JavaVirtual Machine, done at Sun
`Microsystems, Inc., emulated its instruction set in software on a handheld
`device that resembled a contemporary Personal Digital Assistant (PDA). Sun’s
`current Java release, the Java Developer’s Kit (JDK) version 1.0.2, emulates the
`Java Virtual Machine on Win32, MacOS, and Solaris platforms. However, the
`Java Virtual machinedoes not assumeany particular implementation technology
`or host platform. It is not inherently interpreted, and it may just as well be
`implemented by compiling its instruction set to that of a real CPU,as for a con-
`ventional programming language. It may also be implemented in microcode, or
`directly in silicon.
`TheJavaVirtual Machineknows«nothingoftheJavaprogramminglanguage,
`only of aparticularfileformat,theclassfileformat. A class file contains Java
`Virtual Machine instructions (or bytecodes) and a symboltable, as well as other
`ancillary information.
`
`Page 4 of 27
`
`Page 4 of 27
`
`
`
`INTRODUCTION
`
`For‘the sake of security, the Java Virtual Machine imposes strong format and
`structural constraints on the code in a class file. However, any language with
`functionality that can be expressed in terms of a valid class file can be hosted by
`the Java Virtual Machine. Attracted by a generally available, machine-indepen-
`dent platform, implementors of other languages are turning to the Java Virtual
`Machineas a delivery vehicle for their languages. In the future, we will consider
`bounded extensions to the Java Virtual Machine to provide better support for other
`languages.
`
`Summary of Chapters
`
`The rest of this book is structured as follows:
`
`Chapter 2 gives an overview of Java concepts and terminology necessary for
`the rest of the book.
`
`Chapter 3 gives an overview of the Java Virtual Machine.
`
`Chapter 4 defines the class file format, a platform- and implementation-
`independentfile format for compiled Java code.
`
`Chapter 5 describes runtime managementof the constant pool.
`
`Chapter 6 describesthe instruction setof the Java Virtual Machine, presenting
`the instructions in alphabetical order of opcode mnemonics.
`
`Chapter 7 gives examples of compiling Java code into the instruction set of the
`Java Virtual Machine.
`
`Chapter 8 describes Java Virtual Machine threads and their interaction with
`memory.
`:
`
`Chapter 9 describes an optimization used by Sun’s implementation of the Java
`Virtual Machine. While notstrictly part of the specification,it is a useful tech-
`nique in itself, as well as an example of the sort of implementation technique
`that may be employed within a Java Virtual Machine implementation.
`
`Chapter 10 gives a table of Java Virtual Machine opcode mnemonics indexed
`by opcode value.
`
`Page 5 of 27
`
`Page 5 of 27
`
`
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`Use of Fonts
`
`In this book, fonts are used as follows:
`
`+ A fixed width font is used for code examples written in Java, Java Virtual
`Machine data types, exceptions, and errors.
`
`* Italic is used for Java Virtual Machine “assembly language,” its opcodes and
`operands, as well as items in the Java Virtual Machine’s runtime data areas.It
`is also used to introduce new terms, and simply for emphasis.
`
`Page 6 of 27
`
`Page 6 of 27
`
`
`
`CHAPTER 2
`
`
`
`Java Concepts
`
`Tue Java Virtual Machine was designed to support the Java programming lan-
`guage. Some concepts and vocabulary from the Java language are thus necessary
`to understand the virtual machine. This chapter gives enough of an overview of
`Java to support the discussion of the Java Virtual Machine to follow. Its material
`has been condensed from The Java Language Specification, by James Gosling,
`Bill Joy, and Guy Steele. For a complete discussion of the Java language, or for
`details and examples of the material in this chapter, refer to that book. Readers
`familiar with that book may wish to skip this chapter. Readers familiar with Java,
`but not with The Java Language Specification, should at least skim this chapter
`for the terminologyit introduces.
`This chapter does not attempt to provide an introductionto or a full treatment of
`the Java language. For an introduction to Java, see The Java Programming Lan-
`guage, by Ken Arnold and James Gosling.
`
`2.1 Unicode
`
`Java programsare written using the Unicode character encoding, version 1.1.5, as
`specified in The Unicode Standard: Worldwide Character Encoding, Version 1.0,
`Volume 1, ISBN 0-201-56788-1, and Volume 2, ISBN0-201-60845-6, and the
`update information about Unicode 1.1.5 available at ftp: //unicode.org. There
`are a few minor errors in this update information; refer to The Java Language
`Specification for corrections. Updates to the Unicode information published there
`will be posted under the URL http: //java.sun.com/Series.
`Except for comments and identifiers (§2.2) and the contents of character
`and string literals ($2.3), all input elements in a Java program are formed from
`only ASC/T characters. ASCII (ANSI X3.4) is the American Standard Code for
`
`Page 7 of 27
`
`Page 7 of 27
`
`
`
`Information Interchange. The first 128 characters of the Unicode character
`encoding are the ASCII characters.
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`2.2
`
`Identifiers
`
`An identifier is an unlimited-length sequence of Unicode /etters and digits, the
`first of which mustbe a letter. Letters and digits may be drawn from the entire
`Unicode character set, which supports most writing scripts in use in the world
`today. This allows Java programmersto use identifiers in their programsthat are
`written in their native languages.
`The Java method Character.isJavaLetter returns true when passed a
`Unicode character that is considered to bealetter in Java identifiers. The Java
`method Character. isJavaLetterOrDigit returns true when passed a Uni-
`code characterthat is considered to be a letter or digit in Java identifiers.
`Twoidentifiers are the same only if they have the same Unicode character for
`_
`eachletter or digit; identifiers that have the same external appearance maystill be
`different. An identifier must not be the same as a Java keywordora boolean literal
`(true or false).
`
`2.3
`
`Literals
`
`A literal is the source code representation of a value of a primitive type (82.4.1),
`the String type (§2.4.7), or the null type (§2.4). String literals and, more gener-
`ally, strings that are the values of constant expressions, are “interned” so as to
`share unique instances, using the method String. intern.
`The null type has one value, the null reference, denotedby the literal nu11.
`The boolean type has two values, denoted by the literals true and false.
`
`2.4
`
`Types and Values
`
`Java is a strongly typed language, which means that every variable and every
`expression has a type that isknown at compile time. Types limit the values that a
`variable (§2.5) can hold or that an expression can produce,limit the operations
`supported on those values, and determine the meaningof those operations. Strong
`typing helps detect errors at compile time.
`
`Page 8 of 27
`
`Page 8 of 27
`
`
`
`JAVA CONCEPTS
`
`The types of the Java language are divided into two categories: primitive types
`(§2.4.1) and reference types (§2.4.5). There is also a special null type, the type of
`the expression nu11, which has no name. The null reference is the only possible
`value of an expression of null type, and can always be converted to any reference
`type. In practice, the Java programmercan ignore the null type and just pretend
`that nu11 is a special literal that can be of any reference type.
`Corresponding to the primitive types and reference types, there are two cate-
`gories of data values that can be stored in variables, passed as arguments, returned
`by methods, and operated upon: primitive values (§2.4.1) and reference values
`(§2.4.5).
`
`2.4.1
`
`Primitive Types and Values
`
`A primitive type is a type that is predefined by the Java language and named by a
`reserved keyword. Primitive values do not share state with other primitive values.
`A variable whose type is a primitive type always holds a primitive value of that
`type.!
`The primitive types are the boolean type and the numeric types. The numeric
`types are the integral types and thefloating-point types.
`The integral types are byte, short, int, and long, whose values are 8-bit,
`16-bit, 32-bit, and 64-bit signed two’s-complement integers, respectively, and
`char, whose values are 16-bit unsigned integers representing Unicode characters
`($2.1).
`.
`The floating-point types are float, whose values are 32-bit IEEE 754 float-
`ing-point numbers, and double, whose values are 64-bit IEEE 754 floating-point
`numbers as specified in JEEE Standard for Binary Floating-Point Arithmetic,
`ANSI/TEEE Standard 754-1985 (IEEE, New York). The IEEE 754 standard
`includes not only positive and negative sign—magnitude numbers, but also positive
`and negative zeroes, positive and negative infinities, and a special Not-a-Number
`(hereafter abbreviated NaN) value. The NaN value is used to represent the result
`of certain operations such as dividing zero by zero.
`The boolean type hasthe truth values true and false.
`
`|. Note thata local variable is notinitialized on its creation, and is only considered to hold a value
`once it is assigned to (§2.5.1).
`
`Page 9 of 27
`
`Page 9 of 27
`
`
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`2.4.2 Operators on Integral Values
`
`Java provides a numberof operatorsthat act on integral values, including numerical
`comparison (which results in a value of type boolean), arithmetic operators, incre-
`ment and decrement, bitwise logical and shift operators, and numeric cast (§2.6.8).
`Operandsof certain unary andbinary operators are subject to numeric promo-
`tion (§2.6,.9),
`:
`The built-in integer operators do notindicate overflow or underflow in any
`way; they wrap around on overflow or underflow. The only integer operators that
`can throw an exception are the integer divide and integer remainder operators,
`which can throw an Ari thmeticExceptionif the right-hand operandis zero.
`Any value of any integral type maybecast to or from any numeric type. There
`‘are no casts between integral types and the type boolean.
`
`2.4.3 Operators on Floating-Point Values
`
`Java provides a numberof operators that act on floating-point values, including
`numerical comparison (which results in a value of type boolean), arithmetic
`operators, increment and decrement, and numeric cast (§2.6.8).
`If at least one of the operands to a binary operatoris of floating-point type,
`then the operationis a floating-point operation, even if the other operandis inte-
`gral. Operands of certain unary and binary operators are subject to numeric pro-
`motion (§2.6.9),
`Operators on floating-point numbers behave exactly as specified by IEEE
`754. In particular, Java requires support of IEEE 754 denormalized floating-point
`numbers and gradual underflow, which make it easier to prove desirable proper-
`ties of particular numerical algorithms.
`|
`Java requires that floating-point arithmetic behave as if every floating-point
`operator rounded its floating-point result to the result precision. Inexact results
`must be roundedto the representable value nearestto the infinitely precise result;
`if the two nearest representable values are equally near, the one with its least sig-
`nificant bit zero is chosen. This is the IEEE 754 standard’s default rounding mode
`knownas round-to-nearest.
`Java uses round-towards-zero mode when converting a floating-point value to
`an integer (§2.6.3), Round-towards-zero modeacts as though the number were trun-
`cated, discarding the mantissa bits. Round-towards-zero choosesasits result the for-
`‘mat’svalue closest to and nogreater in magnitude than the infinitely precise result.
`
`Page 10 of 27
`
`Page 10 of 27
`
`
`
`JAVA CONCEPTS
`
`Java floating-point operators produce no exceptions (§2.15). An operation that
`overflows produces a signed infinity; an operation that underflows produces a
`signed zero; and an operation that has no mathematically definite result produces
`NaN.All numeric operations (except for numeric comparison) with NaN as an ~
`operand produce NaNasa result.
`Any value of any floating-point type may be cast (§2.6.8) to or from any
`numeric type, There are no casts between floating-point types and the type
`boolean.
`
`2.4.4 Operators on boolean Values
`
`The boolean operators include relational operators and logical operators. Only
`boolean expressions can be used in Java’s control flow statements and as the first
`operand of the conditional operator 7?:. An integral value x can be converted to a
`value of type boolean, following the C language convention that any nonzero
`value is true, by the expression x!=@. An object reference obj can be converted
`to a value of type boolean, following the C language convention that any refer-
`ence other than nu11 is true, by the expression obj !=nu11.
`There are no casts between the type boolean and any othertype.
`
`2.4.5 Reference Types, Objects, and Reference Values
`
`There are three kinds of reference types: the class types (§2.8), the interface types
`(§2.13), and the array types ($2.14). An object is a dynamically created class
`instance or an array. The reference values (often just references) are pointers to
`these objects and a special null reference, which refers to no object.
`A class instance is explicitly created by a class instance creation expression,
`or by invoking the newInstance method of class Class.-An array is explicitly
`created by an array creation expression. An objectis created in the Java heap, and
`is garbage collected after there are no more references to it. Objects are never
`reclaimedor freed by explicit Java languagedirectives.
`There may be many references to the same object. Most objects have state,
`stored in the fields of objects that are instances of classes or in the variables that
`are the components of an array object. If two variables contain references to the
`same object, the state of the object can be modified using one variable’s reference
`to the object, and then the altered state can be observed through the other vari-
`able’s reference.
`
`Page 11 of 27
`
`Page 11 of 27
`
`
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION ;
`
`Each object has an associated Jock (§2.17, $8.13) that is used by synchroni zed
`methods and by the synchronized statement to provide control over concurrent
`access to state by multiple threads (§2.17, $8.12).
`Reference types form a hierarchy. Each class type is a subclass of another
`class type, except for the class Object (§2.4.6), which is the superclass (§2.8.3)
`of all other class types. All objects, including arrays, support the methodsofclass
`Object. String literals (§2.3) are references to instances of class String ($2.4.7).
`
`2.4.6
`
`The Class Object
`
`The standard class Object is the superclass (§2.8.3) of all other classes. A vari-
`able of type Object can hold a reference to any object, whetherit is an instance of
`a class or an array. All class and array types inherit the methods of class Object.
`
`2.4.7.
`
`The Class String
`
`Instances of class String represent sequences of Unicode characters (§2.1). A
`String object has a constant, unchanging value. String literals (§2.3) are refer-
`ences to instances of. class String.
`
`2.4.8 Operators on Objects
`
`The operators on objects include field access, method invocation, cast, string con-
`catenation, comparison for equality, instanceof, and the conditional operator ?:.
`
`2.5 Variables
`
`A variable is a storage location. It has an associated type, sometimescalled its
`compile-time type, that is either a primitive type (§2.4.1) or a reference type
`(§2.4.5). A variable always contains a value that
`is assignment compatible
`(§2.6.6) with its type. A variable of a primitive type always holds a value of that
`exact primitive type. A variable of reference type can hold either a null reference
`or a reference to any object whose class is assignment compatible (§2.6.6) with
`the type of the variable.
`Compatibility of the value of a variable with its type is guaranteed by the
`design of the Java language because default values (§2.5.1) are compatible and
`all assignments to a variable are checked, at compile time, for assignment com-
`patibility.
`
`Page 12 of 27
`
`Page 12 of 27
`
`
`
`JAVA CONCEPTS
`
`There are seven kinds of variables:
`
`11
`
`. A class variable is a field of a class type declared using the keyword static
`(§2.9.1) within a class declaration, or with or without the keyword static in
`an interface declaration. Class variables are created when the class or interface
`is loaded (§2.16.2) and are initialized on creation to default values (§2.5.1).
`The class variable effectively ceases to exist when its class or interface is
`unloaded (§2.16.8) after any necessary finalization of the class (§2.16.8) has
`been completed.
`
`. An instance variableis a field declared within a class declaration withoutusing
`the keyword static (§2.9.1). If a class Thasa field a that is an instance vari-
`able, then a new instance variable a is created and initialized to a default value
`(§2.5.1) as part of each newly created object of class T or of any class that is a
`subclass of 7. The instance variable effectively ceases to exist when the object
`of whichitis a field is no longer referenced, after any necessary finalization of
`the object (§2.16.7) has been completed.
`
`Les)
`
`. Array components are unnamed variables that are created and initialized to
`default values (§2.5.1) whenever a new object that is an array is created
`($2.16.6). The array components effectively cease to exist whenthe array is no
`longer referenced.
`
`. Methodparameters name argumentvalues passed to a method. For every param-
`eter declared in a method declaration, a new parametervariable is created each
`time that methodis invoked. The new variableis initialized with the correspond-
`ing argument value from the method invocation. The method parameter effec-
`tively ceases to exist when the execution of the body of the method is complete.
`
`Wn
`
`Constructor parameters name argument values passed to a constructor. For
`every parameter declared in a constructor declaration, a new parametervari-
`able is created each time a class instance creation expression or explicit con-
`structor invocation is evaluated. The new variable is initialized with the
`corresponding argument value from the creation expression or constructor
`invocation. The constructor parametereffectively ceases to exist when the exe-
`cution of the body of the constructor is complete.
`
`. An exception-handler parameter variable is created each time an exceptionis
`caught by a catchclause of a try statement (§2.15.2). The new variable is ini-
`tialized with the actual object associated with the exception (§2.15.3). The
`exception-handler parameter effectively ceases to exist when execution of the
`block associated with the catch clause (§2.15.2) is complete.
`
`Page 13 of 27
`
`Page 13 of 27
`
`
`
`12
`
`7.
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`Local variables are declared by local variable declaration statements. When-
`ever the flow of control enters a block or a for statement, a new variable is cre-
`ated for each local variable declared in a local variable declaration statement
`immediately contained within that block or for statement. The local variable
`is not initialized, however, until the local variable declaration statement that
`declares it is executed. The local variable effectively ceases to exist when the
`execution of the block or for statement is complete.
`
`2.5.1
`
`Initial Values of Variables
`
`Every variable in a Java program must have a value before it is used:
`
`Eachclass variable, instance variable, and array componentis initialized with
`a default value whenit is créated:
`
`For type byte, the default value is zero, that is, the value of (byte) @.
`
`For type short, the default value is zero, that is, the value of (short) @.
`
`For type int, the default value is zero, that is, @.
`
`For type long, the default value is zero, that is, OL.
`
`For type float, the default value is positive zero, that is, @. Of.
`
`For type double, the default value is positive zero, that is, @. @d.
`
`For type char, the default value is the null character, that is,
`
`'\u@000".
`
`For type boolean, the default value is false.
`
`Forall reference types (§2.4.5), the default value is nu11 (§2.3).
`
`A
`
`Each method parameter (§2.5) is initialized to the corresponding argument
`value provided by the invoker of the method.
`
`Each constructor parameter ($2.5) is initialized to the corresponding argument
`value provided by an object creation expression or explicit constructor invoca-
`tion.
`
`An exception-handler parameter (§2.15.2) is initialized to the thrown object
`representing the exception (§2.15.3).
`
`A local variable must be explicitly given a value beforeit is used, by either ini-
`tialization or assignment.
`
`Page 14 of 27
`
`Page 14 of 27
`
`
`
`JAVA CONCEPTS
`
`13
`
`2.5.2 Variables Have Types, Objects Have Classes
`
`Every object belongs to some particular class. This is the class that was mentioned
`in the class instance creation expression that produced the object, or the class
`whose class object was used to invoke the newInstance method to produce the
`object. This class is called the class of the object. An object is said to be an
`instance of its class and ofall superclasses of its class. Sometimes the class of an
`abject is called its “runtime type,” but “class” is the more accurate term.
`(Sometimes a variable or expression is said to have a “runtime type,” but that
`is an abuse of terminology; it refers to the class of the object referred to by the
`value of the variable or expression at run time, assuming that the value is not
`null. Properly speaking, type is a compile-time notion. A variable or expression
`has a type; an object or array has no type, but belongstoaclass.)
`The type of a variable is always declared, and the type of an expression can be
`deduced at compile time. The type limits the possible values that the variable can
`hold or the expression can produce at run time. If a runtime value is a reference
`that is not nu11, it refers to an objector array that has a class (not a type), andthat
`class will necessarily be compatible with the compile-time type.
`Even though a variable or expressionmay have a compile-time type that is an
`interface type, there are no instances of interfaces ($2.13). A variable or expres-
`sion whose type is an interface type can reference any object whose class imple-
`ments that interface.
`Every array also has a class. The classes for arrays have strange names that
`are not valid Java identifiers; for example, the class for an array of int compo-
`nents has the name “[I”.
`
`2.6 Conversions and Promotions
`
`Conversions implicitly change the type, and sometimes the value, of an expres-
`sion to a type acceptable for its surrounding context. In some cases this will
`require a corresponding action at run time to check the validity of the conversion
`or to translate the runtime value of the expression into a form appropriate for the
`new type.
`Numeric promotions are conversions that change an operand of a numeric
`Operation to a wider type, or both operands of a numeric operation to a common
`type, so that an operation can be performed.
`
`Page 15 of 27
`
`Page 15 of 27
`
`
`
`14
`
`THE JAVA™ VIRTUAL MACHINE SPECIFICATION
`
`In Java, there are six broad kinds of conversions:
`
`Identity conversions
`
`Widening primitive conversions
`
`Narrowing primitive conversions
`
`Widening reference conversions
`
`Narrowing reference conversions
`
`String conversions
`
`Thereare five conversion contexts in which conversion expressions can occur.
`Each context allows conversions in some of the above-named categories but not
`others. The conversion contexts are:
`
`Assignmentconversion (§2.6.6), which converts the type of an expression to
`the type of a specified variable. The conversions permitted for assignment
`are limited in such a way that assignment conversion never causes an excep-
`tion.
`
`Methodinvocation conversion (§2.6.7), which is applied to each argumentin
`a methodor constructor invocation, and, except in one case, performs the same
`conversions that assignment conversion does. Method invocation conversion
`never causes an exception.
`.
`
`Casting conversion (§2.6.8), which converts the type of an expression to a type
`explicitly specified by a cast operator. It is more inclusive than assignment or
`method invocation conversion, allowing any specific conversion other than a
`string conversion, but certain casts to a reference type may cause an exception
`at run time.
`
`String conversion, which allowsany type to be converted to type String (§2.4.7).
`
`Numeric promotion, which brings the operands of a numeric operator to a
`commontype so that an operation can be performed.
`
`String conversion only applies to operands of the binary + operator when one
`of the arguments is a String; it will not be covered further.
`
`2.6.1
`
`Identity Conversions
`
`A conversion from a type to that same type is permitted for any type.
`
`Page 16 of 27
`
`Page 16 of 27
`
`
`
`JAVA CONCEPTS
`
`2.6.2 Widening Primitive Conversions
`
`The following conversions on primitive types are called the widening primitive
`conversions:
`
`* byte to short, int, long, float, or double
`* short to int, long, float, or double
`
`* char to int, long, float, or double
`
`* int to long, float, or double
`
`*
`
`long to float or double
`
`* float to double
`
`Widening conversions do not lose information aboutthe sign or order of mag-
`nitude of a numeric value. Conversions widening from an integral type to another
`integral type and from float to double do not lose any information at all; the
`numeric value is preserved exactly. Conversion of an int or a long value to
`Float, or of a long value to double, maylose precision, that is, the result may
`lose some of the least significant bits of the value; the resulting floating-point
`value is a correctly rounded version of the integer value, using IEEE 754 round-
`to-nearest mode (§2.4.3).
`Accordingto this rule, a widening conversion of a signed integer value to an
`integral type simply sign-extends the two’s-complement representation of the
`integer valueto fill the wider format. A widening conversion of a value of type
`char to an integral type zero-extends the representation of the character value to
`fill the wider format.
`that loss of precision may occur, widening conversions
`Despite the fact
`amongprimitive types neverresult in a runtime exception (§2,15).
`
`2.6.3 Narrowing Primitive Conversions
`
`The following conversions on primitive types are called narrowing primitive con-
`versions:
`:
`
`* byte to char
`
`* short to byte or char
`
`* char to byte or short
`
`* int to byt