Primitive type conversion can be done through promotion or type casting. Type casting can cause loss of information.
- Every Java data item has a type.…At some point we'll have to copy data items of one type…to variables items of another type.…There are three categories of type conversions.…Between primitive types,…between reference types,…and between primitive types and wrapper class types.…Let's take a look at a few examples of do's…and do not when it comes to type conversions.…I'll say this in general,…it's always possible to do a conversion from narrower…to wider data types.…
If you undergo the opposite way…and force the issue you can use type casting.…I'll show you examples of that as well.…Here in line one we define a char variable…with the letter a in it.…In line two we assign the value of…the variable a to an integer.…What would the output look like?…Generally speaking, assigning values of a narrower type…to variables of a wider type is allowed.…In this case the narrower type is char.…
Java automatically promotes the narrower types…to the wider types.…In this example when we assign a to b,…the ASCII code value of letter a is promoted to an int…
Released
2/8/2019- Data types
- Allowable operations
- Literals and primitives
- Prefixes and suffixes
- Reference types
- Identifier rules
- Conversions
- Casting
Share this video
Embed this video
Video: Primitive type casting