Why is char c = (char)65.8;
allowed in Java?
Shouldn't it throw an error since 65.8
is not an exact Unicode value? I understand that the double is truncated to an integer, in this case, 65
, but it seems like bad design to me to allow the programmer to make such a cast.
We can convert double to String in java using String. valueOf() and Double.
#1) Typecasting In this way of conversion, double is typecast to int by assigning double value to an int variable. Here, Java primitive type double is bigger in size than data type int. Thus, this typecasting is called 'down-casting' as we are converting bigger data type values to the comparatively smaller data type.
This is most commonly occurs with the numeric data types . But boolean primitive type can never be used in a cast. Its values must be either true or false and cannot be used in a casting operation.
Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.
That is called Narrowing type casting.
From oracle docs
:
22 specific conversions on primitive types are called the narrowing primitive conversions:
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.
In Java, there are two basic types of type conversions: widening and narrowing.
A widening conversion
occurs when you convert from a type with smaller (or narrower) to type with larger (or wider) range. Because of this, there is no chance for data loss and the conversion is considered "safe."
A narrowing conversion
occurs when you convert from a type with larger (or wider) to type with smaller (or narrower) range. Since we are shrinking the range, there is a chance of data loss so this conversion is considered "unsafe"
The conversion from byte to char is a special case and represents widening and narrowing at the same time. The conversion starts by converting the byte to an int and then the int gets converted to the char.
One reason I can think of why narrowing type casting doesn't result in an error/exception is to allow for a convenient/easy/quick type conversion in the cases when no data will be loss. Compiler leaves it up to us to make sure converted data will be able to fit in the smaller range. It is also useful if we want to quickly truncate values such as rounding the value of a double
(by type-casting it to an int
).
it doesn't happen automatically on assignment: that would be a compilation error.
The fact that the programmer makes a conscious choice (e.g. the type cast) means she is taking into consideration the possibility of, and responsibility for, possible truncation.
You may have code such as cipher algorithms that may find useful to cast a double
or float
to char
. Also, char
is an unsigned type, which means (char)200.5
yields something different than (char)(byte)200.5
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With