Is there cast in Java similar to <reinterpret_cast> in C++

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


No, you can use serialization instead.


There is no way in Java to have an arbitrary block of bytes and then tell the compiler "you need to treat this as an object of type X".

How were those bytes that you want to "restore into an object" created in the first place?

Java has a serialization mechanism to convert objects to a stream of bytes and vice versa.


I'm not exactly sure what you're asking here, but each object in Java (and this includes arrays) has runtime type information associated with it. So when you cast an object to a different type, an exception is thrown right away if the new type doesn't match. This is very different from C/C++ where you can just tell the compiler to treat a block of memory as whatever object you want it to be.

If you're looking for code to convert an arbitrary set of bytes into an object or vice-versa you'll need to do it a different way, either using the built-in serialization facilities or else rolling your own conversion code.


No, you need to serialize your object. http://java.sun.com/developer/technicalArticles/Programming/serialization/

This may not be useful if your object data is expected to be readable in other languages.