I get in my function message array of bytes and type of object, I need to restore object from bytes. Is there in Java any cast like in C++ ?
reinterpret_cast
can be simulated in Java to a fair degree, though not for directly deserializing an entire object. As others have suggested, Java's built-in serialization mechanism or other third-party serialization libraries would be your friend there, unless you yourself have the luxury of decomposing your object into simpler types on the sending end.
As for the specific question of whether Java has an equivalent of reinterpret_cast
, the answer is a partial yes. If you are trying to read primitives from a binary data stream, ByteBuffer
is your friend. Wrap it around an array of bytes, tell it whether the data is little endian or big endian, and then use its various methods to decode the data. This can be used to simulate various C++ reinterpret_cast
conversions from bytes into 32-bit integers or floats or whatever.