备孕怀男孩做什么准备| 及是什么意思| 南京区委书记什么级别| 收留是什么意思| 飞机杯有什么用| 生扶什么意思| 脑萎缩是什么病| 指日可待什么意思| 胎盘老化是什么原因造成的| 桂花像什么| 春天有什么花| 金牛座后面是什么星座| 取保候审需要什么条件| 117是什么电话| 丁字五行属什么| 女生适合喝什么茶| 什么时候做人流才是最佳时间| 虎眼石五行属什么| 男命正官代表什么| 心脏不好吃什么药最好| 低钾是什么原因造成的| 有里面没有两横是什么字| 黑棕色是什么颜色| 松花蛋不能和什么一起吃| 下午7点是什么时辰| 狗是什么意思| 痛经吃什么药| 脾胃寒湿吃什么中成药| 试管是什么意思| 什么蔬菜补铁效果最好| 小孩晚上睡不着是什么原因| 什么还珠成语| 孕妇拉肚子是什么原因引起的| 质问是什么意思啊| 雪碧喝多了有什么危害| 嗔恨心是什么意思| 胆囊息肉样病变是什么意思| 白细胞偏高有什么危害| 回族不吃什么| 菠菜不能和什么一起吃| pt是什么时间| 摸底是什么意思| 什么原因引起脑梗| 睡觉老做梦是什么原因| 撤退性出血什么意思| 2013年是什么命| 谈什么色变| 微波炉里不能放什么| 唐氏综合症是什么意思| 熬夜到什么程度会猝死| 点状血流信号是什么意思| 喉炎用什么药| 叶子像什么| 砼为什么念hun| 血压为什么会高| 辐射对称是什么意思| 2月9号什么星座| ch表示什么意思| 浑身疼吃什么药管用| 腹胀是什么原因引起的| 心烦意乱焦躁不安吃什么药| 男人喜欢什么礼物| 内膜b型是什么意思啊| 令是什么生肖| 芬必得是什么药| 未免是什么意思| 雾化治疗的作用是什么| 葛根主治什么病| 晚上一点多是什么时辰| 姨妈没来是什么原因| 什么的遐想| 什么是元气| 山药补什么| 国防部长什么级别| 心脏有个小洞叫什么病| 夫人是什么生肖| 主胰管不扩张是什么意思| 尿后余沥是什么意思| 运动喝什么水补充能量| 鹦鹉代表什么生肖| sanyo是什么牌子| 心悸心慌是什么原因| 头痛应该挂什么科| 孕早期适合吃什么食物| lafuma是什么牌子| 棚户区改造和拆迁有什么区别| 足外翻挂什么科| 伽马射线是什么| 浪花像什么| 宫颈活检cin1级是什么意思| 热辐射是什么| 医是什么结构的字| 慢性盆腔炎吃什么药效果好| 吧唧嘴什么意思| 带子是什么海鲜| 人间烟火什么意思| 容易口腔溃疡什么原因| 梦见火灾预示什么| 咖啡不能和什么一起吃| 脾阳不足吃什么中成药| 久视伤血是什么意思| gap什么意思| 高尿酸血症吃什么药| 属猴配什么属相最好| 男孩过生日送什么礼物好| 蛇和什么属相相冲| 南乳和腐乳有什么区别| 一个人自言自语的说话是什么病| polo是什么意思| 辛弃疾字什么号什么| 梦见死鸡是什么预兆| 冬瓜什么季节吃最好| 梦见打麻将是什么意思| 鱼喜欢吃什么食物| 力所能及什么意思| 梦到插秧是什么意思| 9点是什么时辰| 羲字五行属什么| 什么是欲望| 一什么不什么的成语| 脚趾头抽筋是什么原因| 舌头上有溃疡是什么原因| 晚上多梦是什么原因| 胃疼吃什么食物| 罗非鱼吃什么食物| 上皮细胞是什么意思| 阴道红肿是什么原因| 自渎什么意思| 鸡蛋白过敏指的是什么| 舌苔厚发白是什么原因| 咽峡炎是什么病| 眼睛干涩用什么药效果好| 2.0是什么意思| 生死有命富贵在天什么意思| 平仓什么意思| 6月3号什么星座| 肠鸣是什么原因| 炸酥肉用什么粉| 灰指甲挂什么科| 医保和农村合作医疗有什么区别| 胃疼需要做什么检查| 鱼上浮的原因是什么| 浩浩荡荡是什么意思| 8月28日什么星座| 耳后长痣代表什么意思| 怀孕牙龈出血是什么原因| 星星像什么| 享福是什么意思| 下焦湿热是什么意思| 家有喜事指什么生肖| 碱是什么| 轮廓是什么意思| vpc是什么| loewe是什么意思| 可尔必思是什么饮料| 闭经是什么意思| 寸是什么单位| 营卫是什么意思| 中国国菜是什么菜| 十指纤纤尽夸巧是什么生肖| 2月18什么星座| 冰山一角是什么生肖| 孩子注意力不集中去医院看什么科| 早入簧门姓氏标什么意思| 阳起石是什么东西| 什么是植物| 羊鞭是什么部位| 孕妇可以吃什么| 医学检验是干什么的| 数是什么意思| 金箔金命是什么意思| 什么是三伏贴| 甲沟炎应该挂什么科| fdp偏高是什么原因| 便秘吃什么药效果好| 山竹和什么不能一起吃| 什么什么归赵| 生理需要是什么意思| 甲亢挂什么科| 山药和什么不能一起吃| 曹曦月演过什么电视剧| 6月16日是什么日子| 移花接木什么意思| 才思敏捷是什么意思| 中元节注意什么| 皮试阳性是什么意思| 诸事不宜是什么意思| 为什么印度叫阿三| 白醋和白米醋有什么区别| 白加黑是什么颜色| 华为最新款手机是什么型号| 114514什么意思| 贫血四项是指什么检查| 阴毛长虱子用什么药| 螺吃什么| 黍米是什么米| 孕妇血压低吃什么能补上来| 小鸟站在高压线上为什么不会触电| 朱门是什么意思| 猫咪黑下巴是什么原因| 吃什么能提升血小板| get什么意思| 特需号是什么意思| 卡鱼刺去医院挂什么科| 丙球是什么| 2023是什么年| 生男生女取决于什么| 急性呼吸道感染是什么引起的| 中性粒细胞百分比偏低什么意思| 查五行缺什么| 什么水果清热去火| 很man是什么意思| 1978年什么命| 白酒优级和一级有什么区别| 戊肝是什么病| 心悸心慌是什么原因| 前列腺是什么东西| 吥是什么意思| 感冒了吃什么食物最好| 3s是什么意思| 谷草转氨酶偏高是什么原因| 树挪死人挪活是什么意思| 肠癌吃什么药| 梦见被追杀预示什么| 柠檬有什么功效和作用| 橡皮泥可以做什么| 梦见戴手表是什么意思| 血糖高适合吃什么主食| braun是什么品牌| e6是什么意思| 肝阳性是什么意思| 脾虚吃什么药| 脊灰疫苗是预防什么的| 阳绿翡翠属于什么级别| 三十七岁属什么生肖| 窜稀是什么意思| 孕妇缺铁对胎儿有什么影响| 1980属什么| 什么体质人容易长脚气| 现在领结婚证需要什么| 乳房胀痛是什么原因引起的| 音节是指什么| 女人吃什么能活血化瘀| 七月份出生是什么星座| 高压氧治疗有什么作用| 主家是什么意思| 瘦脸针的危害有什么副作用| 什么地眨眼| 一个虫一个卑念什么| 朊病毒是什么| 嗓子疼喝什么茶最有效| 肌电图主要检查什么病| 子宫瘢痕憩室是什么病| 肾透析是什么意思| 杂合变异是什么意思| 甲状腺有什么症状| 小孩晚上磨牙是什么原因引起的| 4月29号0点是什么时候| 卵巢囊肿是什么| 甲醛中毒吃什么药解毒| 不完全性右束支传导阻滞是什么意思| 音欠读什么| 便秘用什么药好| 洛阳有什么好吃的| 百度

“网信河北”融媒体平台创新融合报“两会”

百度 如何充分发挥调查研究对谋划工作、科学决策的辅助作用,习近平同志在《之江新语》中有精到论述,“各级领导干部在调查研究工作中,一定要保持求真务实的作风,努力在求深、求实、求细、求准、求效上下功夫”。

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 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.

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
甲沟炎应该挂什么科 脸色暗沉发黑什么原因 4月8号什么星座 骨质疏松是什么意思 止咳吃什么药
江西老表是什么意思 客家人什么意思 腺肌症吃什么药 cc是什么意思 威海是什么海
四维空间是什么 肺不好挂什么科 色达在四川什么地方 4.19是什么星座 成全是什么意思
米诺地尔有什么副作用 7月25日是什么星座 胃镜取活检意味着什么 孕妇晚餐吃什么比较好 印迹杂交技术检查什么
透析到什么程度会死亡hcv8jop9ns2r.cn 冬天喝什么茶好呢hcv8jop9ns4r.cn 面基是什么意思hcv9jop5ns5r.cn 为什么会有耳石症hcv8jop9ns2r.cn 7.4是什么星座hcv9jop5ns7r.cn
在什么情况下最容易怀孕hcv8jop5ns7r.cn 外痔是什么样子的hcv8jop0ns3r.cn 参事是什么级别hcv8jop9ns3r.cn 什么是hp感染xianpinbao.com 蛇头是什么意思hcv8jop9ns5r.cn
阑尾炎有什么症状cl108k.com 化工厂是干什么的hcv8jop8ns6r.cn 阴虱长什么样子图片hcv9jop4ns7r.cn 槟榔肝是由什么引起的hcv8jop2ns5r.cn 英雄难过美人关是什么生肖hcv8jop5ns4r.cn
羊与什么相冲hcv9jop5ns6r.cn palace什么牌子hcv8jop0ns0r.cn 诸葛亮是个什么样的人hcv8jop6ns0r.cn 9个月宝宝玩什么玩具hcv7jop4ns7r.cn 定海神针是什么意思bysq.com
百度