11

What is the Java equivalent of following C++ code?

 float f=12.5f;
 int& i = reinterpret_cast<int&>(f);
Raedwald
  • 46,613
  • 43
  • 151
  • 237
ivorykoder
  • 1,000
  • 3
  • 14
  • 32
  • Did you check if the code runs successfully?? it should be `int& i = r_c(f)` – UltraInstinct Jan 01 '10 at 17:06
  • 3
    It is not really translatable into any language. It is interpreting the bits of the float as an int!!! What does that mean? Because the representation of floating point is undefined by the standard it only has meaning if you understand what floating point representation is used by your compiler/hardware here and know that this maps to the Java floating point representation. – Martin York Jan 01 '10 at 17:31
  • 3
    In addition to what Martin York said, it is technically performing an *implementation-defined mapping* from float reference to int reference. Strictly speaking, there's no guarantee that the resulting int reference will alias the same object. – jalf Jan 01 '10 at 17:52
  • See also http://stackoverflow.com/questions/4805058/is-there-cast-in-java-similar-to-reinterpret-cast-in-c – Raedwald Mar 24 '16 at 09:36

1 Answers1

26
float f = 12.5f;
int i = Float.floatToIntBits(f);
missingfaktor
  • 90,905
  • 62
  • 285
  • 365