What is the Java equivalent of following C++ code?
float f=12.5f;
int& i = reinterpret_cast<int&>(f);
A float value can be assigned to an integer variable but an implicit conversion occurs when compiler forces a float value to be assigned as an integer. The digits after the decimal notation in the float value get lost after assigning a float to an integer.
reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different. It does not check if the pointer type and data pointed by the pointer is same or not.
float f = 12.5f;
int i = Float.floatToIntBits(f);
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