Difference between casting and promotion


What is casting ? What is promotion ? What is the basic difference between these types of Java translation?

asked by anonymous 07.12.2015 / 11:40

1 answer


Promotion is the elevation of the numeric types in the language to a type that may represent greater magnitude. The rules of what can be promoted are specified in the language and always indicate that a primitive type of lower numerical capacity can be used in a place where a higher capacity type is needed. Promotion never causes data loss. It is implicitly done by the compiler.

casting is the change from one type to another explicitly through the casting operator (the type name in parentheses). There may be a change in the representation of the value or not, and in this conversion there may be data loss. A run-time error will occur if the conversion can not be performed or the compiler can detect the impossibility of the operation before. casting may or may not change the value representation, which does not run on types by reference.

Documentation for more information.

Note that this is only valid for Java. Other languages have their own rules and terminology.

07.12.2015 / 11:59