What is typecasting? In Java

2007-07-31 9:19 pm
What is the definition of typecast in java?

回答 (1)

2007-08-03 10:33 am
✔ 最佳答案



Java's Type Casting



A cast expression converts, at run time, a value of one numeric type to a similar value of another numeric type; or confirms, at compile time, that the type of an expression is boolean; or checks, at run time, that a reference value refers to an object whose class is compatible with a specified reference type.



Syntax:







CastExpression:

( PrimitiveType Dimsopt ) UnaryExpression
( ReferenceType ) UnaryExpressionNotPlusMinus




See here for a discussion of the distinction between UnaryExpression and UnaryExpressionNotPlusMinus.



The type of a cast expression is the result of applying capture conversion to the type whose name appears within the parentheses. (The parentheses and the type they contain are sometimes called the cast operator.) The result of a cast expression is not a variable, but a value, even if the result of the operand expression is a variable.



A cast operator has no effect on the choice of value set for a value of type float or type double. Consequently, a cast to type float within an expression that is not FP-strict does not necessarily cause its value to be converted to an element of the float value set, and a cast to type double within an expression that is not FP-strict does not necessarily cause its value to be converted to an element of the double value set.



It is a compile-time error if the compile-time type of the operand may never be cast to the type specified by the cast operator according to the rules of casting conversion. Otherwise, at run-time, the operand value is converted (if necessary) by casting conversion to the type specified by the cast operator.



Some casts result in an error at compile time. Some casts can be proven, at compile time, always to be correct at run time. For example, it is always correct to convert a value of a class type to the type of its superclass; such a cast should require no special action at run time. Finally, some casts cannot be proven to be either always correct or always incorrect at compile time. Such casts require a test at run time. See here for details.



A ClassCastException is thrown if a cast is found at run time to be impermissible.



Code Sample #1:




圖片參考:http://i206.photobucket.com/albums/bb265/a048042l/typecast01.gif




Here the first compile-time error occurs because the class types Long and Point are unrelated (that is, they are not the same, and neither is a subclass of the other), so a cast between them will always fail.



The second compile-time error occurs because a variable of type EndPoint can never reference a value that implements the interface Colorable. This is because EndPoint is a final type, and a variable of a final type always holds a value of the same run-time type as its compile-time type. Therefore, the run-time type of variable e must be exactly the type EndPoint, and type EndPoint does not implement Colorable.



Code Sample #2:




圖片參考:http://i206.photobucket.com/albums/bb265/a048042l/typecast02.gif




This example compiles without errors and produces the output:



cpa: { (2,2)@12, (4,5)@24, null, null }



Reference: The Java Language Specification, Third Edition — Expressions — Cast Expressions


參考: 個人經驗


收錄日期: 2021-04-26 13:40:56
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070731000051KK02080

檢視 Wayback Machine 備份