This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes byteValue() method of Integer Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "byteValue() method of Integer Class" Watch "byteValue() method of Integer Class" New topic
Author

byteValue() method of Integer Class

Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
Thanks for reading my post.

The description provided in the Java doc for the method is:
Returns the value of this Integer as a byte.


It is obvious that one byte can only represent upto a value of 255. If the value contained is larger than 255, there would be a loss of data.

My question is why the Java API developers did not provide something like a byte array rather than a byte as the return type. Am I missing someting here?
[ October 31, 2006: Message edited by: Isuru Sampath ]

No Winds No Waves
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Java byte varies from -128 to 127, not 0 to 255.

All the wrapper classes for numeric primitives implement Number interface, which allows conversions between the types. Where perfect conversion is not possible, some data is lost, as you say.

You can get each byte of a short, int or long, using bitwise operations (right-shift then AND).

You can get the bits of a double using Double.toLongBits(), which gives you a long. If you need the individual bytes, use the above technique to get them.

Similar thing applies for float and Float.toIntBits().


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
Thanks Peter. U clarified a lots of doubts I had.

Meanwhile, I found this code snippet on sun java forum which can easily convert an Int to a byte arry. Re-posting it for the advantage of others. Credit goes to the original writer of the code.



Nifty ha?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Nifty .. ?



That's all you need.

EDIT:

This was the way to convert Integer to byte[]. If you want to convert int (the primitive) to byte[], then just put the int in an Integer:

[ October 31, 2006: Message edited by: Bauke Scholtz ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

But Bauke, your code does something entirely different from what Isuru's code does.

Bauke's code converts the int to a string of characters (text), and then converts the string in to an array of bytes that represent the digits in whatever character encoding is the default on the platform you run it on. Suppose the encoding is ASCII or UTF-8, an int with the value 342876 would be converted to a byte array of 6 bytes: 51, 52, 50, 56, 55, 54

What Isuru means is that he wants the 32 bits of the int converted into 4 bytes: 0, 5, 59, 92 (in big-endian order).

Isuru: OK, the code with the ByteArrayOutputStream works but it seems a bit overkill to me. You could just do some bit manipulations:

That's faster, doesn't involve creating ByteArrayOutputStream and DataOutputStream objects, and also avoids the need for users of the method to deal with the IOException.

Nifty ha?
[ October 31, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Originally posted by Jesper Young:
What Isuru means is that he wants the 32 bits of the int converted into 4 bytes: 0, 5, 59, 92 (in big-endian order).
Where can I read this?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

Originally posted by Bauke Scholtz:
Where can I read this?

That's what the code he posted above with the ByteArrayOutputStream and DataOutputStream does.
Isuru Sampath
Ranch Hand

Joined: Jun 26, 2003
Posts: 57
Very many thanks to Jesper and Bauke.

Yes Jesper, what I wanted was the binary representation of the int in Big Endean order. And the code snippet you provided is really nifty and guess it is really fast since bit shifting is the fastest operations we can get (my belief :roll: ).

But the reason I like the previous code is that it does not have to have the byte array length hard coded. It will create a byte array to hold the input integer whatever it's size. I 100% agree with you that it is kind of overkill if the integer size is limited or known. On the other hand if the integer size is dynamic, the previous code snippet is ideal. Guess the choice should be based on the requirement.

All this... pretty nifty and interesting stuff.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14192
    
  20

In Java, an int is always 32 bits, doesn't matter what system you're running it on. Even if you're running it on a 16-bit or 64-bit processor or operating system, and int is 32 bits (unlike C/C++, where the size of an int is system-dependent).

So writing code that's independent of the size of an int is irrelevant in Java...
 
GeeCON Prague 2014
 
subject: byteValue() method of Integer Class