3

Byte value is between 0 and 255, very simple and straight forward. Java didn't think so however and decided that values to be between -128 and 128 and ruined my life and many others.

I just want to know what's the big idea? why everytime i need to get the unsigned byte value do I have to do this:

int byteValue = (int) javaByte & 0xFF;
razz
  • 9,770
  • 7
  • 50
  • 68

1 Answers1

1

Java supports only signed integers. byte is integer number as well so Java is just consistent here.

This is not case for C for example where char type doesn't have defined whether it's signed or unsigned. You need to explicitly tell. This is in my opinion much worse than saying that it's by default signed.

Anyway, the original intention in C for char was a string character where the unsigned made sense. In Java there is different type for string character - char.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • In Pascal, which predated C, values of type Byte were stored as unsigned, but arithmetic on them was performed as signed as a result of promotion to Integer. Many C implementations behaved likewise except when "char" and "int" were the same size. When assembling values out of smaller pieces (as is often done with bytes) unsigned types are easier to work with for all but the uppermost chunk; for any number containing two or more chunks, the extra effort required by making the lower bytes signed will exceed any work saved by making the upper byte signed. – supercat Sep 26 '16 at 22:23