Chinaunix首页 | 论坛 | 博客
  • 博客访问: 198854
  • 博文数量: 69
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-03 11:35
文章分类

全部博文(69)

文章存档

2011年(13)

2010年(46)

2009年(10)

我的朋友

分类: Java

2010-07-12 09:09:41

JLS100712Note on JLS, Ch5

 

@ http://zcatt.cublog.cn

 

Hist:

100712, draft

 

 

 

Chapter5 Conversions and Promotions(转换)

A specific conversion from type S to type T allows an expression of type S to be treated at compile time as if it had type T instead. In some cases this will require a corresponding action at run time to check the validity of the conversion or to translate the run-time value of the expression into a form appropriate for new type T. For example,

1)      a conversion from type Object to type Thread requires a run-time check.

2)      a conversion from type Thread to type Object requires no run-time action.

The specific conversions in Java are grouped into several braod categories:

1)      identity conversions

2)      widening primitive conversions

3)      narrowing primitive conversions

4)      widening reference conversions

5)      narrowing reference covnersions

6)      boxing conversions

7)      unboxing conversions

8)      unchecked conversions

9)      capture conversions

10)   string conversions

11)   value set conversions

There are 5 conversion contexts in which conversion of expressions may occur. The term ‘conversion’ is also used to describe the process of choosing a specific conversion for such a context:

1)      assignment conversion

2)      method invocation conversion

3)      casting conversion

4)      string conversion

5)      numeric promotion

 

Identity Conversions

A conversion from a type to that same type is permitted for any type.

 

Widening Primitive Conversion

There are 19  widening primitive conversions:

1)      byte to short, int, long, float, or double

2)      short to int, long, float, or double

3)      char to int, long, float, or double

4)      int to long, float, or double

5)      long to float or double

6)      float to double

 

Narrowing Primitive Conversions

There are 22 narrowing primitive conversions:

1)      short to byte or char

2)      char to byte or short

3)      int ot byte, short, or char

4)      long to byte, short, char, or int

5)      float to byte, short, char, int or long

6)      double to byte, short, char, int, long, or float

 

Widening and Narrowing Primitive Conversions

byte ot char

First, the byte is converted to an int, and then to char.

 

 

Widening Reference Conversions

Convert from type S to type T, provided S is a subtype of T.

Widening reference conversions never required a special action at run time and never throw an exception at run time.

 

 

Narrowing Reference Conversions

1)      from type S to type T, provided S is a proper supertype of T

2)      from class type C to non-parameterized interface type K, provided that C is not final and does not implement K.

3)      from interface type J to non-parameterized class type C that is not final.

4)      from the interface types Cloneable and java.io.Serializable to any array type T[]

5)      from any interface type J to any non-parameterized interface type K, provided J is not a subinterface of K.

6)      from any array type SC[] to any array type TC[], provided there is a narrowing conversion from SC to TC.

 

Boxing Conversion

Boxing conversion converts values of primitive type to corresponding values of reference type:

1)      from boolean to Boolean

2)      from byte to Byte

3)      from char to Character

4)      from short to Short

5)      from int to Integer

6)      from long to Long

7)      from float to Float

8)      from double to Double

 

Unboxing Conversion

Unboxing conversion converts values of reference type to corresponding values of primitive type.

1)      from Boolean to boolean

2)      from Byte to byte

3)      from Character to char

4)      from Short to short

5)      from Integer to int

6)      from Long to long

7)      from Float to float

8)      from Double to double

 

Unchecked Conversion

Let G name a generic type declaration with n formal type parameters. There is an unchecked conversion from the raw type G to any parameterized type of the form G. Use of an unchecked conversion generates a mandatory compile-time warning unless the parameterized type G is a parameterized type in which all type arguments are unbounded wildcards.

Unchecked conversion is used to enable a smooth interoperation of legacy code, written before the introduction of generic types.

 

Capture Conversion (JLS 5.1.10)

Capture conversion is complex, and I try to explain it in other article. In one word, capture conversion is designed to make wildcards more useful.

 

String Conversions

 

Forbidden conversion

Any conversion that is not explicitly allowed is forbidden.

 

Value Set Conversion

Value set conversion is the process of mapping a floating-point value from one value set to another without changing its type.

 

 

Assignment Conversion

Allow the use of one of the following:

1)      an identity conversion

2)      a widening primitive conversion

3)      a widening reference conversion

4)      a boxing conversion optionally followed by a widening reference conversion

5)      an unboxing conversion optionally followed by a widening primitive conversion.

If after the conversion listed above have been applied, the resulting type is a raw type, unchecked conversion may then be applied.

If the type of an expression can be converted to the type of a variable by assignment conversion, we say the expression is assignable to the variable, and the expression is assignment compatible with the type of the variable. If not, a ClassCastException is thrown.

 

Method Invocation Conversion

Method invocation conversion is applied to each argument value in a method or constructor invocation: the type of the argument expression must be converted to the type of the corresponding parameter. It allows the use of one of the following:

1)      an identity conversion

2)      a widening primitive conversion

3)      a widening reference conversion

4)      a boxing conversion optionally followed by widening reference conversion

5)      an unboxing conversion optionally followed by a widening primitive conversion

 

String Conversion

String conversion applies only to the operands of the binary + operator when one of the argument is a String.

 

Casting Conversion

Casting conversion is applied to the operand of a cast operator: the type of the operand expression must be converted to the type explicitly named by the cast operator. Detail description in uncheck and check rules.

 

Numeric Promotions

 

REF

1.       JLS 3nd

2.       JVM 2nd
Locations of visitors to this page

阅读(901) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~