blob: 6467694003cdcd39f3d5479383947a8eb006c833 [file] [log] [blame]
*Chapter 4*
h1. Types, Values, and Variables
The organization of this chapter parallels the chapter on [Types, Values, and Variables|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html] in the [Java Language Specification (second edition)|http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html], which begins as follows:
bq. The Java programming language is a ??strongly typed?? language, which means that every variable and every expression has a type that is known at compile time. Types limit the values that a variable ([§4.5|Chapter 04 Types#4.5], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#18470]) can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong typing helps detect errors at compile time.
bq. The types of the Java programming language are divided into two categories: primitive types and reference types. The primitive types ([§4.2|Chapter 04 Types#4.2], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#85587]) are the {{boolean}} type and the numeric types. The numeric types are the integral types {{byte}}, {{short}}, {{int}}, {{long}}, and {{char}}, and the floating-point types {{float}} and {{double}}. The reference types ([§4.3|Chapter 04 Types#4.3], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9317]) are class types, interface types, and array types. There is also a special null type. An object ([§4.3.1|Chapter 04 Types#4.3.1], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#12028]) is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to objects. All objects, including arrays, support the methods of class {{Object}} ([§4.3.2|Chapter 04 Types#4.3.2], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#11055]). String literals are represented by {{String}} objects ([§4.3.3|Chapter 04 Types#4.3.3], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#26992]).
bq. Names of types are used ([§4.4|Chapter 04 Types#4.4], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#25948]) in declarations, casts, class instance creation expressions, array creation expressions, class literals, and {{instanceof}} operator expressions.
bq. A variable ([§4.5|Chapter 04 Types#4.5], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#18470]) is a storage location. A variable of a primitive type always holds a value of that exact type. A variable of a class type _T_ can hold a null reference or a reference to an instance of class _T_ or of any class that is a subclass of _T_. A variable of an interface type can hold a null reference or a reference to any instance of any class that implements the interface. If _T_ is a primitive type, then a variable of type "array of _T_" can hold a null reference or a reference to any array of type "array of _T_"; if _T_ is a reference type, then a variable of type "array of _T_" can hold a null reference or a reference to any array of type "array of _S_" such that type _S_ is assignable ([§5.2|Chapter 05 Conversions#5.2], [JLS|http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#184206]) to type _T_. A variable of type {{Object}} can hold a null reference or a reference to any object, whether class instance or array.
{anchor:4.1}
h2. 4.1 The Kinds of Types and Values
(Cf. ??[JLS. §4.1|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#11128]??.)
TO DO
{anchor:4.2}
h2. 4.2 Primitive Types and Values
(Cf. ??[JLS. §4.2|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#85587]??.)
TO DO
{anchor:4.2.1}
h3. 4.2.1 Integral Types and Values
(Cf. ??[JLS. §4.2.1|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9151]??.)
TO DO
{anchor:4.2.2}
h3. 4.2.2 Integer Operations
(Cf. ??[JLS. §4.2.2|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#29775]??.)
TO DO
{anchor:4.2.3}
h3. 4.2.3 Floating-Point Types, Formats, and Values
(Cf. ??[JLS. §4.2.3|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9208]??.)
TO DO
{anchor:4.2.4}
h3. 4.2.4 Floating-Point Operations
(Cf. ??[JLS. §4.2.4|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9249]??.)
TO DO
{anchor:4.2.5}
h3. 4.2.5 The <code>boolean Type and <code>boolean</code> Values</h3>
(Cf. ??[JLS. &#167;4.2.5|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9295]??.)
TO DO
{anchor:4.3}
h2. 4.3 Reference Types and Values
(Cf. ??[JLS. &#167;4.3|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#9317]??.)
TO DO
{anchor:4.3.1}
h3. 4.3.1 Objects
(Cf. ??[JLS. &#167;4.3.1|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#12028]??.)
TO DO
{anchor:4.3.2}
h3. 4.3.2 The Class Object
(Cf. ??[JLS. &#167;4.3.2|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#11055]??.)
TO DO
{anchor:4.3.3}
h3. 4.3.3 The Class String
(Cf. ??[JLS. &#167;4.3.3|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#27000]??.)
TO DO
{anchor:4.3.4}
h3. 4.3.4 When Reference Types Are the Same
(Cf. ??[JLS. &#167;4.3.4|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#97058]??.)
TO DO
{anchor:4.4}
h2. 4.4 Where Types Are Used
(Cf. ??[JLS. &#167;4.4|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#52309]??.)
TO DO
{anchor:4.5}
h2. 4.5 Variables
(Cf. ??[JLS. &#167;4.5|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10872]??.)
TO DO
{anchor:4.5.1}
h3. 4.5.1 Variables of Primitive Type
(Cf. ??[JLS. &#167;4.5.1|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#28344]??.)
TO DO
{anchor:4.5.2}
h3. 4.5.2 Variables of Reference Type
(Cf. ??[JLS. &#167;4.5.2|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#28345]??.)
TO DO
{anchor:4.5.3}
h3. 4.5.3 Kinds of Variables
(Cf. ??[JLS. &#167;4.5.3|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10883]??.)
TO DO
{anchor:4.5.4}
h3. 4.5.4 final Variables
(Cf. ??[JLS. &#167;4.5.4|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#96639]??.)
TO DO
{anchor:4.5.5}
h3. 4.5.5 Initial Values of Variables
(Cf. ??[JLS. &#167;4.5.5|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#96595]??.)
TO DO
{anchor:4.5.6}
h3. 4.5.6 Types, Classes, and Interfaces
(Cf. ??[JLS. &#167;4.5.6|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#24887]??.)
TO DO
----
Specification [Table of Contents|Chapter 00 Contents].
The organization of this chapter parallels the chapter on [Types, Values, and Variables|http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html] in the [Java Language Specification (second edition)|http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html].
The original of this specification is at [http://docs.codehaus.org/display/GroovyJSR].