0

In C/C++, you can do the following:

struct DataStructure
{
  char member1;
  char member2;
};

DataStructure ds;
char bytes[] = {0xFF, 0xFE};

memcpy(&ds, bytes, sizeof(ds));

and you would essentially get the following:

ds.member1 = 0xFF;
ds.member2 = 0xFE;

What is the Java equivalent?

5
  • You can only do that sort of thing in C++ in general by blithely ignoring all the platform and compiler dependencies that it depends on. Commented Sep 23, 2012 at 4:26
  • A little nitpick: the Java equivalent, if it existed, would probably be converting a byte array to an object, or part of an object, not a class. (You actually can convert a Java byte array to a class if the byte array conforms to the structure of a class definition as described in the Java specification, but that's not what you're asking about.) Commented Sep 23, 2012 at 4:39
  • @DavidZaslavsky that sounds like exactly what I'm asking about. How can I do that? Commented Sep 23, 2012 at 4:55
  • @Brandon no, it's not at all the same thing as what the C code you put in your question does. But if you're curious, the relevant method is ClassLoader.defineClass. Commented Sep 23, 2012 at 5:54
  • Another nitpick: you are not converting from byte to a class or object, you are setting the data members of an object from the data contained in a char array. Commented Sep 23, 2012 at 5:54

4 Answers 4

3

What is the Java equivalent?

There is no Java equivalent.

Java does not allow you to create or modify objects by accessing them at that level. You should be using new or setter methods, depending on what you are trying to achieve.

(There are a couple of ways to do this kind of thing, but they are unsafe, non-portable and "not Java" ... and they are not warranted in this situation.)

Sign up to request clarification or add additional context in comments.

4 Comments

out of curiosity -- and I swear I won't use it in my project -- how can this be done?
In native methods or using the "Unsafe" class. But you really need to know what you are doing, or else you are liable to randomly crash the JVM. And besides, there is no real benefit from resorting to that kind of thing in this situation. It is not simpler, it is not faster, it is not anything good ... that I can think of.
+1 just because there is an obtuse way to do something doesn't make it a good idea. Its highly unlikely to be a good idea in any language IMHO.
Disclaimer: While I use Unsafe more than anyone I have met, I still wouldn't use it for this sort of thing.
0

The memcpy you wrote depends on the internal implementation of the struct and would not necessarily work. In java, you need to define a constructor that accepts a byte array and set the fields. No shortcuts like this, as the memory structure of the class is not defined.

Comments

0

In Java you cannot work with the memory directly (no memcpy) it is the advantage (disadvantage?) of Java. There are some java library methods to copy arrays: System.arraycopy(). In general, to copy some object you need to ship it with clone method.

Comments

0

You might be able to do that in C. But you'd be wandering into aliasing problems and a hunka hunka burning undefined behavior.

And because struct padding is up to a compiler, what you might get with your memcpy is just ds.member1 = 0xFF, ds.member2 = whatever junk happened to be on the stack at the time, because member1 was padded to occupy 4 bytes rather than just 1. Or maybe you get junk for both, because you set the top 2 bytes of a 4-byte and they're in the bottom 2 bytes.

What you're wandering into is compiler/runtime-specific memory layouts. The same is true in Java. Java itself won't let you do something so horrendously un-Java, but if you write your own JVM or debug an existing JVM written in C or C++, you could do something like that. And who knows what would happen; I'm not Java god enough to know exactly how much the JVM spec pins down JVM implementation, but my guess is, not to the degree necessary to enable interoperability of the in-memory, runtime representations of objects.

So you get undefined behavior in every language flavor. Tastes just as good in each language, too - like mystery meat.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.