Is there cast in Java similar to in C++

后端 未结 7 984
小蘑菇
小蘑菇 2020-12-04 01:54

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++ ?

相关标签:
7条回答
  • 2020-12-04 02:53

    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.

    0 讨论(0)
提交回复
热议问题