Difference between casting and Promotion

Asked

Viewed 755 times

13

What is casting? What is Promotion? What is the basic difference between these Java conversion types?

1 answer

11


To promotion is the elevation of numerical types existing in language to a type that may represent a greater greatness. The rules of what can be promoted are specified in the language and always indicate that a primitive type* lower numerical capacity can be used in a place where a type with higher capacity will be required. Promotion never causes data loss. It is made implicitly by the compiler.

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

Documentation for more information.

Note that this is only valid for Java. Other languages have their own rules and terminology. I don’t know how you get with the advent of structs in Java.

*This idea of primitive will get complicated when Java has basic data so it is not primitive (and will soon). And I want to see how they’ll handle casting since they have decided not to have operator overload, will change their mind or will be something more capenga?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.