分类: Java
2010-07-09 16:29:25
Hist:
100709, draft
A strongly typed means that every variable and every expression has a type that is known at compile time. There are two categories of types in Java language, primitive types and reference types. The primitive types are the Boolean type and the numeric types, byte, short, int, long, char, float and double. The reference types are class types, interface types, and array types. There is also a special null type. An object is a dynamically created instance of a class type or a dynamically created array. The values of a reference type are references to object.
Integeral Types and values
byte, [-128, 127]
short, [-32768, 32767]
int, [-2147483648, 2147483647]
long, [–9223372036854775808,
9223372036854775807]
char, [\u0000, \uffff]
Integer operations
< <= > >= == != + - * / % ++ -- << >> >>>
~ & | ^ ?:
cast operator
string concatenation operator +
Floating-poing types, formats, and values
IEEE754 standard.
The boolean type and values
true and false
operators: == != ! & ^ | && || ?:
string concatenation operator +
Reference types and values
Objects
An object is a class instance or an array. The reference values (often
just references) are pointers to these objects.
A class instance is explicitly created by a class instance creation
expression. An array is explicitly created by an array creation expression.
The operators on references to objects are:
1)
field access
2)
method invocation
3)
the cast operator
4)
string concatenation operator+
5)
instanceof
6)
the reference equality operators == and !=
7)
the conditional operator ?:
There may be many references to the same object.
The Class Object
The class Object is a superclass of all other classes. A variable of type
Object can hold a reference to the null reference or to any object. All class
and array types inherit the methods of class Object.
The Class String
Instance of class String represent sequences of Unicode characters. A String object has a constant(unchanging) value.
When Reference Types Are the Same(引用类型的相同是什么
Two reference types are the same compile-time type if they have the same binary name and their type parameters, if any, are the same, applying this definition recursively. When two reference types are the same, they are sometimes said to be the same class or the same interface.
Two reference types are the same run-time type if:
1) They are both class or both interface types, are defined by the same class loader, and have the same binary name, in which case they are sometimes said to be the same run-time class or the same run-time interface.
2) They are both array types, and their component types are the same run-time type.
Type Variables(类型变量)
A type variable is an unqualified identifier.
such as:
T extends C
T extends I
T extends C &I1 &I2 &In
The members of a type variable T with bound C & I1 I2 In are the members of the intersection type C& I1 I2 In appearing at the point where the type variable is declared.
Parameterized Types(参数化类型)
A parameterized type consist of a class or
interface name C and actual type argument list
Two parameterized types are provable distinct if either of the following conditions hold:
1) They are invocations of distinct generic type declarations.
2) Any of their type arguments are provable distinct.
Type Argument and Wildcards(类型参数和通配符)
Type arguments may be either reference types or wildcards.
such as:
>
extends T>
super T>
Type Argument Containment and Equivalence(类型参数的包含和相等含义)
A type argument TA1 is said to contain another type argument TA2, written TA2<=TA1, if the set of types denoted by TA2 is provably a subset of the set of types denoted by TA1 under the following rules(where <: denotes subtyping)
1) ? extends T <= ? extends S if T<:S
2) ? super T <= ? super S if S<:T
3) T<=T
4) T <= ? extends T
Members and Constructors of Parameterized Types(参数类型情况下的成员变量和构造函数)
Let C be a class or interface declaration
with formal type parameters A1,...,An, and let C
Type Erasure(类型擦除)
Type erasure is a mapping from types(maybe including parameterized types and type variables) to types(never parameterized types or type variables). We write |T| for the erasure of type T.
1)The erasure of a parameterized type
G
2)The erasure of a nested type T.C is |T|.C
3)The erasure of an array type T[] is |T|[]
4)The erasure of a type variable is the erasure of its leftmost bound.
5)The erasure of every other type is the type itself.
The erasure of a method signature s is a signature consisting of the same name as s, and the erasures of all the formal parameter types given in s.
Reifiable Types(可具体化类型)
Because some type information is erased during compilation, not all types are available at run time. Types that are completely available at run time are known as reifiable types.
Reifiable iff:
1) It refers to a non-generic type delaration
2) It is a parameterized type in which all type arguments are unbounded wildcards
3) It is a raw type
4) It is a primitive type
5) It is an array type whose component type is reifiable.
Raw Types
Raw type is the type which is the erasure of the parameterized type. that is, a raw type is defined to be either:
1) The name of a generic type declaration used without any accompanying actual type parameters
2) Any non-static type member of a raw type R that is not inherited from a superclass or superinterface of R
Intersection Types
Subtyping
The subtype and supertype relations are binary relations on types.
direct supertype: S >1 T
supertype: S :> T
proper supertype: S>T
direct subtype: T <1 S
subtype: T <: S
proper subtype: T < S
Where Types Are Used
Types are used when they appear in declarations or in certain expressions.
1) Imported types
2) Fields
3) Method parameters
4) Method results
5) Constructor parameters
6) Local variables
7) Exception handler parameters
8) Type variables
and in expressions of the following kinds:
1) class instance creation
2) generic class instance creations
3) array creations
4) generic method
5) cast
6) the instanceof operator
Types are also used as arguments to parameterized types.
Variables
A variable is a storage location and has an associated type, sometimes called its compile-time type, that is either a primitive type or a reference 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 a subclass or subinterface of type T.
Heap Pollution
It is possible that a variable of a parameterized type refers to an object that is not of that parameterized type. This situation is known as heap pollution. This situation can only occur if the program performed some operation that would give rise to an unchecked warning at compile-time.
Kinds of Variables
There are 7 kinds of variables:
1) a class variable is a field declared using the keyword static within a class declaration, or with or without the keyword static within an interface declaration.
2) an instance variable is a field declared within a class declaration without using the keyword static.
3) array components are unnamed variables that created and initialized to default values whenever a new object that is an array is created.
4) method parameters name argument values passed to a method.
5) constructor parameters
6) an exception-handler parameter is created each time an exception is caught by a catch clause of a try statement.
7) local variables
final Varaibles
A final variable may only be assigned to once. A blank final is a final variable whose declaration lacks an initializer.
If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.
Initial Values of Variables
Variable must have a value before its value is used.
1) each class variable, instance variable, or array component is initialized with a default value when it is created. The default value is null for all reference types.
2) a local variable must be explicitly given a value before it is used.
Types, Classes, and Interfaces
The type may be a primitive type or reference type. Reference types include class types and interface types. The term type always is used to refer to a class or an interface.
The compile time type of a variable is always declared. The run-time type 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.
Every array also has a class; the method getClass will return a class object(of class Class) that represents the class of the array.
1. JLS 3nd