什么锅好| loveyourself什么意思| 大浪淘沙下一句是什么| 酸奶不能和什么一起吃| 为什么会突然晕倒| mom是什么意思| 磨牙是什么原因怎么治疗| 正事是什么意思| 三文鱼是什么鱼| 新鲜橄榄菜长什么样子| 空调什么度数最省电| 蚂蚁上树是什么意思| 排卵日是什么时候| 毛尖茶属于什么茶| 考护师需要什么条件| rh血型D阳性是什么意思| 唐僧的袈裟叫什么| 乳头痒用什么药| 急性胃肠炎用什么药| 牙松动了还疼用什么方法处理最好| 人为什么会得阑尾炎| 蜜蜡是什么材质| 脂溢性皮炎头皮用什么洗发水| 后援会是什么意思| 午时是什么时间| 小哥哥是什么意思| 然五行属性是什么| 呕吐是什么原因| 头发为什么会变黄| 嫖娼是什么意思| 小狗拉稀 吃什么药| 除是什么意思| 什么是宫寒| 右肾盂分离是什么意思| 声音的高低叫什么| 彩色多普勒超声检查是什么| 糖尿病是什么| 喝鲜羊奶有什么好处和坏处| lca是什么意思| 组织液是什么| 甲沟炎用什么药| 黄雀是什么鸟| 提手旁加茶念什么| 放疗跟化疗有什么区别| hpv52阳性是什么病| 菠菜为什么要焯水| 舌头发黄是什么问题| 癫痫病是什么症状| 减脂喝什么茶最有效| 妄想是什么意思| 梦见大蛇是什么意思| 什么鸡蛋营养价值最高| 贬值是什么意思| 提防是什么意思| 什么是音序| 心血虚吃什么中成药| 咽炎雾化用什么药最好| 海底捞是什么| 铁皮石斛有什么功效| 姐姐的婆婆叫什么| 指甲开裂是什么原因| 幽门螺杆菌吃什么药最好| 胰腺在人体什么位置| 脚背疼是什么原因| 国家能源局是什么级别| 总蛋白偏高有什么危害| 怀孕两个月有什么症状| 空调为什么要加氟| 转氨酶高吃什么好得快| 高尿酸血症吃什么药| 什么的表演| 为什么老是睡不着| 12月10日是什么星座| 山西人喜欢吃什么| pi是什么意思| 肺火旺吃什么药| 属蛇与什么属相相克| 月经期间吃什么水果| 膝盖酸疼是什么原因| 鸟喙是什么意思| 过敏性结膜炎用什么眼药水最好| 疱疹用什么药膏最有效| 日本有什么特产| 睡眠不好吃什么好| 屑是什么意思| 眼色是什么意思| 什么是喜欢什么是爱| 什么原因引起耳鸣| 小腿麻木是什么原因| 高血压的人不能吃什么| 男性左下腹痛是什么原因| 甲状腺结节什么引起的| 忽视是什么意思| 血管堵塞用什么药| 嘴唇上火起泡用什么药| 前庭大腺囊肿是什么原因引起的| 舌苔厚白用什么泡水喝| 尿酸吃什么药最有效果| 腋下出汗是什么原因| 母乳是什么颜色| 鼻屎有臭味是什么原因| 白细胞一个加号什么意思| 记性差是什么原因| 低密度脂蛋白高的原因是什么| poc是什么| 什么叫腔梗| 尿崩症吃什么药最有效| 牛有几个胃分别叫什么| 洗衣机脱水是什么意思| 手术后为什么要平躺6小时| 什么叫慢阻肺| 酒精过敏什么症状| 血小板为0意味着什么| 心衰吃什么药效果最好| 什么欢快| 手掌麻是什么原因引起的| cd4是什么意思| 西瓜跟什么不能一起吃| 老人肚子胀是什么原因| 398是什么意思| 丙五行属什么| 生闷气是什么意思| 空巢老人什么意思| 沉香木是什么树| 吃什么补充黄体酮| 吃什么增肥| 为什么会得飞蚊症| 什么是单核细胞百分比| 血压低是什么原因| ein是什么意思| babyface是什么意思| 妥投是什么意思| rpe是什么意思| 粘土是什么土| 什么可以祛痘印| 油墨用什么可以洗掉| 局座是什么梗| 自言自语的近义词是什么| 玉米什么季节成熟| 委曲求全是什么生肖| 运钞车是什么车| 部分空蝶鞍是什么意思| 周杰伦的粉丝叫什么| 睾丸是什么形状的| 狐假虎威告诉我们什么道理| instagram什么意思| 艾草长什么样| 虎头蜂泡酒有什么功效| 别出心裁是什么生肖| 葡萄酒中的单宁是什么| 凭什么| 梦见钱包丢了是什么意思| 湿气重吃什么中药| 阴虚吃什么中药| 为什么怀孕前三个月不能说| 心脏彩超ef是什么意思| 暂告一段落是什么意思| 脾大吃什么可以缩脾| 64岁属什么生肖| 宝宝咬人是什么原因| 药流后需要注意什么| strong什么意思| 调理是什么意思| 霖字五行属什么| 咒怨讲的是什么故事| 生死劫是什么意思| 什么叫失眠| 韭黄炒什么好吃| 胸小是什么原因| 透明的什么填词语| 羊鞭是什么部位| 化疗和放疗有什么区别| 吃什么增加孕酮| 吃b族维生素有什么好处| 刮腻子是什么意思| bug是什么意思网络用语| 送什么礼物好| 1月3日什么星座| 颈椎病用什么枕头好| 农历9月14日是什么星座| 消纳是什么意思| 吃茄子对身体有什么好处| 牙冠什么材质的好| 外痔长什么样| 大便里面有血是什么原因| 眉毛少是什么原因| 吃什么补铁快| 什么话是世界通用的| 金刚是什么意思| 至死不渝下一句是什么| 天蝎座是什么星象| 子宫后倾位是什么意思| 夏季种什么菜| clinic是什么意思| 什么茶不能喝| oem贴牌是什么意思| 右耳朵热代表什么意思| 娇气是什么意思| 油面筋是什么做的| crp什么意思| pm什么意思| 鸡粉是什么| 什么地溜达| 潘驴邓小闲什么意思| 怀孕吃什么必定流产| 女孩子命硬有什么表现| 32年婚姻是什么婚| pi是什么意思| 得了梅毒会有什么症状| 狗属于什么类动物| 澳大利亚说什么语| 梦见墙倒了有什么预兆| 肺炎不能吃什么东西| 盗汗遗精是什么意思| 半胱氨酸是什么| 梦见自己怀孕大肚子是什么预兆| 奶冻是什么| zv是什么品牌| 乳贴是什么| 什么字寓意好| 米豆腐是什么做的| 豺狼虎豹为什么豺第一| 王禹读什么| 睾丸疼是什么原因| 卖淫是什么| 银离子是什么| 去医院点痣挂什么科| 属兔配什么属相最好| 秋香绿是什么颜色| 撕脱性骨折是什么意思| 土阜念什么| 五十是什么之年| 尿酸查什么| 管理的本质是什么| 妇科病是什么| 青提是什么| other是什么意思| 间质性肺病是什么意思| 三月二十是什么星座| 一个鸟一个衣是什么字| 56年属什么生肖| 右肋骨下方是什么器官| 胃糜烂要吃什么药| 什么家| 开除党籍有什么后果| mirage轮胎什么牌子| 什么什么之什么| 中性粒细胞比率偏高是什么意思| 太阳是什么颜色的| 650是什么意思| 男性尿道痒吃什么药| 口腔溃疡什么药最管用| 小211是什么意思| 中元节会开什么生肖| 人造海蜇丝是什么做的| 蛇缠腰是什么症状| cuff是什么意思| 近视用什么镜矫正| 牛黄解毒片不能和什么药一起吃| 侏儒是什么意思| 爽肤水和精华水有什么区别| 小孩积食吃什么| 咽炎吃什么药| 对牛弹琴告诉我们什么道理| 百度Jump to content

孫中山與八田與一銅像均遭破壞 賴清德雙重標準處理

From Wikipedia, the free encyclopedia
百度 持续打好防范化解重大风险、精准脱贫、污染防治三大攻坚战。

In computer science, type conversion,[1][2] type casting,[1][3] type coercion,[3] and type juggling[4][5] are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa. Type conversions can take advantage of certain features of type hierarchies or data representations. Two important aspects of a type conversion are whether it happens implicitly (automatically) or explicitly,[1][6] and whether the underlying data representation is converted from one representation into another, or a given representation is merely reinterpreted as the representation of another data type.[6][7] In general, both primitive and compound data types can be converted.

Each programming language has its own rules on how types can be converted. Languages with strong typing typically do little implicit conversion and discourage the reinterpretation of representations, while languages with weak typing perform many implicit conversions between data types. Weak typing language often allow forcing the compiler to arbitrarily interpret a data item as having different representations—this can be a non-obvious programming error, or a technical method to directly deal with underlying hardware.

In most languages, the word coercion is used to denote an implicit conversion, either during compilation or during run time. For example, in an expression mixing integer and floating point numbers (like 5 + 0.1), the compiler will automatically convert integer representation into floating point representation so fractions are not lost. Explicit type conversions are either indicated by writing additional code (e.g. adding type identifiers or calling built-in routines) or by coding conversion routines for the compiler to use when it otherwise would halt with a type mismatch.

In most ALGOL-like languages, such as Pascal, Modula-2, Ada and Delphi, conversion and casting are distinctly different concepts. In these languages, conversion refers to either implicitly or explicitly changing a value from one data type storage format to another, e.g. a 16-bit integer to a 32-bit integer. The storage needs may change as a result of the conversion, including a possible loss of precision or truncation. The word cast, on the other hand, refers to explicitly changing the interpretation of the bit pattern representing a value from one type to another. For example, 32 contiguous bits may be treated as an array of 32 Booleans, a 4-byte string, an unsigned 32-bit integer or an IEEE single precision floating point value. Because the stored bits are never changed, the programmer must know low level details such as representation format, byte order, and alignment needs, to meaningfully cast.

In the C family of languages and ALGOL 68, the word cast typically refers to an explicit type conversion (as opposed to an implicit conversion), causing some ambiguity about whether this is a re-interpretation of a bit-pattern or a real data representation conversion. More important is the multitude of ways and rules that apply to what data type (or class) is located by a pointer and how a pointer may be adjusted by the compiler in cases like object (class) inheritance.

Explicit casting in various languages

[edit]

Ada

[edit]

Ada provides a generic library function Unchecked_Conversion.[8]

C-like languages

[edit]

Implicit type conversion

[edit]

Implicit type conversion, also known as coercion or type juggling, is an automatic type conversion by the compiler. Some programming languages allow compilers to provide coercion; others require it.

In a mixed-type expression, data of one or more subtypes can be converted to a supertype as needed at runtime so that the program will run correctly. For example, the following is legal C language code:

double  d;
long    l;
int     i;

if (d > i)   d = i;
if (i > l)   l = i;
if (d == l)  d *= 2;

Although d, l, and i belong to different data types, they will be automatically converted to equal data types each time a comparison or assignment is executed. This behavior should be used with caution, as unintended consequences can arise. Data can be lost when converting representations from floating-point to integer, as the fractional components of the floating-point values will be truncated (rounded toward zero). Conversely, precision can be lost when converting representations from integer to floating-point, since a floating-point type may be unable to exactly represent all possible values of some integer type. For example, float might be an IEEE 754 single precision type, which cannot represent the integer 16777217 exactly, while a 32-bit integer type can. This can lead to unintuitive behavior, as demonstrated by the following code:

#include <stdio.h>

int main(void)
{
    int i_value   = 16777217;
    float f_value = 16777216.0;
    printf("The integer is: %d\n", i_value);
    printf("The float is:   %f\n", f_value);
    printf("Their equality: %d\n", i_value == f_value);
}

On compilers that implement floats as IEEE single precision, and ints as at least 32 bits, this code will give this peculiar print-out:

The integer is: 16777217
The float is: 16777216.000000
Their equality: 1

Note that 1 represents equality in the last line above. This odd behavior is caused by an implicit conversion of i_value to float when it is compared with f_value. The conversion causes loss of precision, which makes the values equal before the comparison.

Important takeaways:

  1. float to int causes truncation, i.e., removal of the fractional part.
  2. double to float causes rounding of digit.
  3. long to int causes dropping of excess higher order bits.
Type promotion
[edit]

One special case of implicit type conversion is type promotion, where an object is automatically converted into another data type representing a superset of the original type. Promotions are commonly used with types smaller than the native type of the target platform's arithmetic logic unit (ALU), before arithmetic and logical operations, to make such operations possible, or more efficient if the ALU can work with more than one type. C and C++ perform such promotion for objects of Boolean, character, wide character, enumeration, and short integer types which are promoted to int, and for objects of type float, which are promoted to double. Unlike some other type conversions, promotions never lose precision or modify the value stored in the object.

In Java:

int x = 3;
double y = 3.5;
System.out.println(x + y); // The output will be 6.5

Explicit type conversion

[edit]

Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is requested by the user in the program.

double da = 3.3;
double db = 3.3;
double dc = 3.4;
int result = (int)da + (int)db + (int)dc; // result == 9
// if implicit conversion would be used (as with "result = da + db + dc"), result would be equal to 10

There are several kinds of explicit conversion.

checked
Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.
unchecked
No check is performed. If the destination type cannot hold the source value, the result is undefined.
bit pattern
The raw bit representation of the source is copied verbatim, and it is re-interpreted according to the destination type. This can also be achieved via aliasing.

In object-oriented programming languages, objects can also be downcast : a reference of a base class is cast to one of its derived classes.

C# and C++

[edit]

In C#, type conversion can be made in a safe or unsafe (i.e., C-like) manner, the former called checked type cast.[9]

Animal animal = new Cat();

Bulldog b = (Bulldog) animal;  // if (animal is Bulldog), stat.type(animal) is Bulldog, else an exception
b = animal as Bulldog;         // if (animal is Bulldog), b = (Bulldog) animal, else b = null

animal = null;
b = animal as Bulldog;         // b == null

In C++ a similar effect can be achieved using C++-style cast syntax.

Animal* animal = new Cat;

Bulldog* b = static_cast<Bulldog*>(animal); // compiles only if either Animal or Bulldog is derived from the other (or same)
b = dynamic_cast<Bulldog*>(animal);         // if (animal is Bulldog), b = (Bulldog*) animal, else b = nullptr

Bulldog& br = static_cast<Bulldog&>(*animal); // same as above, but an exception will be thrown if a nullptr was to be returned
                                              // this is not seen in code where exception handling is avoided

delete animal; // always free resources
animal = nullptr;
b = dynamic_cast<Bulldog*>(animal);         // b == nullptr

Eiffel

[edit]

In Eiffel the notion of type conversion is integrated into the rules of the type system. The Assignment Rule says that an assignment, such as x := y, is valid if and only if the type of its source expression (y) is compatible with the type of its target entity (x). In this rule, compatible with means that the type of the source expression either conforms to or converts to that of the target. Conformance of types is defined by the rules for polymorphism in object-oriented programming. For example, in the assignment above, the type of y conforms to the type of x if the class upon which y is based is a descendant of that upon which x is based.

Rust

[edit]

Rust provides no implicit type conversion (coercion) between most primitive types. But, explicit type conversion (casting) can be performed using the as keyword.[10]

let x = 1000;
println!("1000 as a u16 is: {}", x as u16);

Type assertion

[edit]

A related concept in static type systems is called type assertion, which instruct the compiler to treat the expression of a certain type, disregarding its own inference. Type assertion may be safe (a runtime check is performed) or unsafe. A type assertion does not convert the value from a data type to another.

TypeScript

[edit]

In TypeScript, a type assertion is done by using the as keyword:[11]

const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;

In the above example, document.getElementById is declared to return an HTMLElement, but you know that it always return an HTMLCanvasElement, which is a subtype of HTMLElement, in this case. If it is not the case, subsequent code which relies on the behaviour of HTMLCanvasElement will not perform correctly, as in Typescript there is no runtime checking for type assertions.

In Typescript, there is no general way to check if a value is of a certain type at runtime, as there is no runtime type support. However, it is possible to write a user-defined function which the user tells the compiler if a value is of a certain type of not. Such a function is called type guard, and is declared with a return type of x is Type, where x is a parameter or this, in place of boolean.

This allows unsafe type assertions to be contained in the checker function instead of littered around the codebase.

Go

[edit]

In Go, a type assertion can be used to access a concrete type value from an interface value. It is a safe assertion that it will panic (in the case of one return value), or return a zero value (if two return values are used), if the value is not of that concrete type.[12]

t := i.(T)

This type assertions tell the system that i is of type T. If it isn't, it panics.

Implicit casting using untagged unions

[edit]

Many programming languages support union types which can hold a value of multiple types. Untagged unions are provided in some languages with loose type-checking, such as C and PL/I, but also in the original Pascal. These can be used to interpret the bit pattern of one type as a value of another type.

Security issues

[edit]

In hacking, typecasting is the misuse of type conversion to temporarily change a variable's data type from how it was originally defined.[13] This provides opportunities for hackers since in type conversion after a variable is "typecast" to become a different data type, the compiler will treat that hacked variable as the new data type for that specific operation.[14]

See also

[edit]

References

[edit]
  1. ^ a b c Mehrotra, Dheeraj (2008). S. Chand's Computer Science. S. Chand. pp. 81–83. ISBN 978-8121929844.
  2. ^ Programming Languages - Design and Constructs. Laxmi Publications. 2013. p. 35. ISBN 978-9381159415.
  3. ^ a b Reilly, Edwin (2004). Concise Encyclopedia of Computer Science. John Wiley & Sons. pp. 82, 110. ISBN 0470090952.
  4. ^ Fenton, Steve (2017). Pro TypeScript: Application-Scale JavaScript Development. Apress. pp. xxiii. ISBN 978-1484232491.
  5. ^ "Type Juggling". PHP Manual. Retrieved 27 January 2019.
  6. ^ a b Olsson, Mikael (2013). C++ Quick Syntax Reference. Apress. pp. 87–89. ISBN 978-1430262770.
  7. ^ Kruse, Rudolf; Borgelt, Christian; Braune, Christian; Mostaghim, Sanaz; Steinbrecher, Matthias (16 September 2016). Computational Intelligence: A Methodological Introduction. Springer. p. 269. ISBN 978-1447172963.
  8. ^ "Unchecked Type Conversions". Ada Information Clearinghouse. Retrieved 11 March 2023.
  9. ^ M?ssenb?ck, Hanspeter (25 March 2002). "Advanced C#: Checked Type Casts" (PDF). Institut für Systemsoftware, Johannes Kepler Universit?t Linz, Fachbereich Informatik. p. 5. Retrieved 4 August 2011. at C# Tutorial
  10. ^ "Casting". Rust by Example. Retrieved 1 April 2025.
  11. ^ "Everyday Types". The TypeScript Handbook. Retrieved 1 April 2025.
  12. ^ "Type assertions". A Tour of Go. Retrieved 1 April 2025.
  13. ^ Erickson, Jon (2008). Hacking: The Art of Exploitation. No Starch Press. p. 51. ISBN 978-1-59327-144-2. "Typecasting is simply a way to temporarily change a variable's data type, despite how it was originally defined. When a variable is typecast into a different type, the compiler is basically told to treat that variable as if it were the new data type, but only for that operation. The syntax for typecasting is as follows: (typecast_data_type) variable ..."
  14. ^ Gopal, Arpita (2009). Magnifying C. PHI Learning Private Limited. p. 59. ISBN 978-81-203-3861-6. "From the above, it is clear that the usage of typecasting is to make a variable of one type, act like another type for one single operation. So by using this ability of typecasting it is possible for create ASCII characters by typecasting integer to its ..."
[edit]
天秤座后面是什么星座 11.22什么星座 轻度脑萎缩是什么意思 尿频尿多是什么原因 什么姿势睡觉最好
经常犯困想睡觉是什么原因 女人平胸是什么原因 智齿为什么会发炎 孕酮低是什么原因造成的 隐翅虫怕什么
ab型血可以接受什么血型 脑梗是什么病严重吗 焕字五行属什么 先父遗传是什么意思 沅字五行属什么
粘胶是什么材质 来月经是黑色的是什么原因 水钠潴留什么意思 阑尾炎挂什么科室 头发粗硬是什么原因
一六年属什么生肖hcv9jop2ns8r.cn sp02是什么意思hcv9jop3ns6r.cn 道地药材是什么意思hcv8jop0ns5r.cn 87年的兔是什么命hcv9jop5ns6r.cn 一个巾一个占念什么sanhestory.com
合肥为什么叫合肥hcv8jop6ns8r.cn 打玻尿酸有什么副作用吗hcv9jop3ns8r.cn 为什么家里不能放假花hcv7jop9ns1r.cn hpv亚型是什么意思hcv7jop7ns2r.cn 头发里长痣代表什么520myf.com
什么中药治肝病最好hcv8jop6ns7r.cn 做梦流产了是什么意思hcv9jop4ns9r.cn 酥油茶是什么做的kuyehao.com 双子座是什么星座gysmod.com 什么是处男travellingsim.com
自来鸟是什么兆头cj623037.com 正骨有什么好处和坏处hcv7jop7ns4r.cn 多巴胺是什么hcv8jop1ns4r.cn 晕车吃什么hcv8jop8ns0r.cn 去心火喝什么茶好hcv8jop1ns0r.cn
百度