6

I have to admit in all my work with Java, I've never come across the need for a Java union (like the C union, not the SQL one) and I cannot find an answer here on SO. Admittedly, most of my work in Java has been at higher abstractions than bit-fiddling.

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

In C, I'd do something like:

union {
    int i;
    float f;
} x;
x.i = 0x27;
printf ("%f\n", x.f);

How do I do a similar thing in Java? Is it even possible to treat the same memory as two different data types in Java?

I did search on both SO and elsewhere for "java union" but it swamped me with SQL stuff - I couldn't find a way to do this.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953

5 Answers5

12

I have a integer which I'm setting individual bits for and I want to print out the equivalent IEEE754 single-precision float.

Use intBitsToFloat() for that.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • Bang. That's what I was after. And I see the reverse is Float.floatToRawIntBits. Now I just need the Double/Long variant but I assume, if there is one, it'll be in the Double class. I'll go find that. Thanks heaps. – paxdiablo Aug 21 '09 at 07:49
  • 2
    The connection with unions is that they allow multiple datatypes to exists in the same memory location, providing the ability to effectively convert from one type to another. Pax needed to do a similar thing with an int and a float and, coming from a C background, naturally fell to the union approach - but couldn't find how to do this in Java. Luckily for him, his particular scenario is dealt with by the standard libraries. – belugabob Aug 21 '09 at 07:57
  • Yeah, my only other thought was to write the int to a file and read it back as a float. I was pretty certain there was a better way than *that*. Thanks, guys, SO proves its worth yet again. – paxdiablo Aug 21 '09 at 08:00
  • A lovely example for how easy it is to fall into the trap of assuming that in order to achieve X you have to do Y, then ask how to do Y rather than how to achieve X. – Michael Borgwardt Aug 21 '09 at 08:04
  • Ah, gotcha. Yes, that was a rather roundabout question to achieve something simple :) – skaffman Aug 21 '09 at 08:48
  • A lovely example of why I feel out of air when working in Java. unions are brilliant! – Mr. Developerdude Jan 19 '17 at 22:05
9

Substitutes for Missing C Constructs

The designers of the Java programming language chose to omit the union construct because there is a much better mechanism for defining a single data type capable of representing objects of various types: subtyping. A discriminated union is really just a pallid imitation of a class hierarchy.

Includes examples.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Yes, but a class hierarchy is no good here. I want to be able to set it as one type and read it as another. A class hierarchy will force me to set it and read it as the same type. – paxdiablo Aug 21 '09 at 07:42
  • Let's back up - is there a way to change one bit in a float? That's what I'm looking for. I want to be able to set (for example) bits 31, 27, 4, and 0 of the float to see what value that is. – paxdiablo Aug 21 '09 at 07:44
  • int bits = Float.floatToIntBits(aFloat); /* use bitwise operators in the IEEE 754 bit vector */ aFloat = Float.intBitsToFloat(bits); – dfa Aug 21 '09 at 07:46
  • Is the question about float manipulation or unions? The two seems to have nothing to do with each other. – skaffman Aug 21 '09 at 07:48
  • @skaffman, union would be how I would have done it with C. I didn't realize that the Float class provided raw bit conversions, - the second sentence in the question is probably the one that's more relevant. – paxdiablo Aug 21 '09 at 07:52
  • Yeah, I remember unions now... it's been a while since I did any C :) – skaffman Aug 21 '09 at 08:48
  • Saying that a "discriminated union is really just a pallid imitation of a class hierarchy" is just blunt misunderstanding of what a discriminated union is. – AK_ Dec 31 '14 at 16:40
5

Java doesn't provide any way to treat a value as another value on raw bit level - no unions, nothing like reinterpret_cast. However, for your particular case of treating float as int and vice versa, you can use java.lang.Float.floatToIntBits and intBitsToFloat methods.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
4

If you are interested in accessing the bit-level representation of a float, java.lang.Double contains method to do this (doubleToLongBits etc.)

Union'ing between pointer types (or between pointers and their numeric represetantion) would open holes in the type system, so it is strictly impossible.

mfx
  • 7,168
  • 26
  • 29
3

Some useful articles & faq to this subject are,

Union types in Java?

Substitutes for Missing C Constructs

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186