Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through all byte values -128-127 without casting in Java

I need to iterate through all byte values (-128 to 127 inclusive). I could use an int iterator, but then I have to cast each time to byte. Using a byte iterator has the problem that I can't test for b < 128 as it will overflow. I thought of using a while loop and doing the test before incrementing, which is my best solution so far. Is there a better approach?

like image 861
moinudin Avatar asked Jun 30 '12 00:06

moinudin


1 Answers

Java bytes are signed so they have the values -128 to 127. Anyway, you shouldn't worry about casts and extra checks like that because they are trivial to optimize away. In fact, at the JVM level, there's no such thing as a byte variable. It's treated as an int anyway.

like image 170
Antimony Avatar answered Nov 13 '22 06:11

Antimony