tags:

views:

398

answers:

1

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

 float f=12.5f;
 int& i = reinterpret_cast<int&>(f);
+22  A: 
float f = 12.5f;
int i = Float.floatToIntBits(f);
missingfaktor
+1 ........... :)
Prasoon Saurav
thanks man!!!!!
JavaGeek
Out of curiosity, what is the difference between what you posted and a int i = (int)f;?
Alberto Zaccagni
@Montecristo : (int) f in Java is like static_cast<int>(f) in C++.
missingfaktor
Ok... so I think I'll have to look for those two c++ methods to get the whole picture, thanks :)
Alberto Zaccagni
@Montecristo : They are cast operators, not methods.
missingfaktor
@Rahul G: Thanks for the correction, I do not know c++
Alberto Zaccagni
Note the C++ version uses int reference. Thus modifying the float will cause the int to change (in some undefined way). The Java version does not provide that functionality. Every change to 'f' must be followed by a call to floatToIntBits().
Martin York