Operations in our applications
will commonly involve multiple data types. Because of that, we must deal with
type conversions. Now there are implicit type conversions, and those are type
conversions that are performed automatically by the compiler. To see a simple
example of that, we look at something like here we have an integer iVal that
we've assigned the value 50. If I then say long lVal equals iVal, the compiler must
convert that 32‑bit integer iVal into a 64‑bit integer as it's stored into lVal. And there are also explicit
type conversions, and these are conversions that I explicitly perform in my code
and I do that using a cast operator. We see here if I had that long lVal = 50
now, so I'm now assigning a 64‑bit integer the value of 50, if I want to assign that into this
iVal, a 32‑bit integer, I have to explicitly cast it. I must tell part of
that I want to go from that 64‑bit integer into a 32‑bit integer. And I do that by using the type I want to convert to,
in this case, int, putting it inside of parentheses, and putting it next to the
value I want to cast. So, this says, the int in parentheses says, take the lVal
and cast it down to a 32‑bit integer. If we look now at these implicit type conversions,
these are generally what we call widening conversions, and these can be done
automatically because we're moving to a wider data type. So, if I have a 32‑bit
integer and I want to move to a 64‑bit integer, that's wider, right? So, the values can be safely
moved, so those can be done implicitly. And the compiler must make decisions
about how to make these conversions. And the rules are straightforward. If I
have expression with multiple integer sizes in it, maybe have a short and a
long, whatever the largest integer size is what things will be converted to. So,
if I do an operation with a short and the long, the short will be implicitly
cast into a long. If I perform an operation with mixed floating‑point
sizes, so I have a float and a double, it'll always go to the double because
double is the largest floating‑point size. And then if I have an operation that uses integer
types and floating‑point types, the compiler will cast to whatever the largest
floating point in the equation is. So, if I do an operation with a long and a float,
the long will be cast to a float. If I do an operation with a long and a
double, the long will then be cast to a double. Explicit type conversions are
the ones that we explicitly perform in our code using this cast operation. When
we do that, we're taking responsibility for whatever happens as a result of
that type conversion. Because of that, we can perform both widening conversions
and narrowing. So, widening going from a 32 bit to say a 64 bit, narrowing
going from a 64 bit down to a 32 bit. We should just want to be aware that we
know what could potentially happen. If we do an explicit cast from a floating
point to an integer, while floating points can have fractional portions,
integers can't. So, any fractional portion would be dropped when I cast that
float down to an integer. You want to be careful when performing a narrowing
conversion. If I have a 64‑bit integer, it can hold values that are too large to fit into a
32‑bit
integer. So, if I cast that 64 bits to a 32 bit, the program will do it, but if
the value was too large to fit into a 32 bit, you'll get some odd results from
that. So, you want to make sure that when you're casting it, doing a narrowing
cast, that you know that what you're doing is safe. And the last one is just
that you want to be careful when converting from an integer to a floating
point. Because if you have an integer with many significant digits, because of
the way floating points are stored, you could lose some of those significant
digits. The deep details of how these type conversions occur are beyond the
scope of this course, but if you'd like to understand them in more detail, I've
got that URL on the screen for you that actually points to the Java
documentation that talks about those details. If you check that out, you'll
particularly want to look at the sections on widening primitive conversion and
narrowing primitive conversion.
Demo: Type Conversion
Comments
Post a Comment
We appreciate your valuable Suggestions/Feedback