0
private static byte [] header = {0x16,0x98,0x01,0x02,0x14,0x01,0x00,0x02,0x10,0x03,0x03};

I have initialized one of my byte array in a java program. Here java showing one error, 0x98 need to be casted to byte externally. Why java showing this value is a int value ? And how to tell java that this is a byte ?

3 Answers3

3

byte has a range from -128 to 127 (=0x7F). (See also https://www.w3schools.com/java/java_data_types.asp)

Therefore the maximum Hex-Number you can add to your byte-array is 0x7F. The next value 0x80 can be stored to int (4 byte) or others like short (2 byte)

Edit:

If you want to assign 0x98 to a byte-variable you can cast it to byte:

byte x = (byte) 0x98;

To print this value unsigned you can use Byte.toUnsignedInt(x);

System.out.println(x); //will print -104
System.out.println(Byte.toUnsignedInt(x)); //will print 152
csalmhof
  • 1,820
  • 2
  • 15
  • 24
  • what is the data type similar to unsigned byte ? – Sodrul Amin Shaon Mar 18 '21 at 07:29
  • I'm not 100% sure but i think there is nothing similar to unsigned byte. If you want to add 0x98 to your byte array you can write code like `byte x = (byte) 0x98`. 0x98 = -104 in that case. When you want to print the value of the byte, `System.out.println(x)` will print -104, if you want to print the unsigned value you can print it like that: `System.out.println(Byte.toUnsignedInt(x));` – csalmhof Mar 18 '21 at 07:37
2

In Java, numerals are assumed to have the type int. So when you write 0x98, this is assumed to be the int 152. If you write 0x03 it is assumed to be the int 3.

The byte type has values ranging from -128 ... 127. You can assign any int in this range to a variable constrained to hold byte variables without any issue.

But the number 152 is outside this range, so you get the error:

incompatible types: possible lossy conversion from int to byte

You can write (byte)0x98 to make it work.

In JShell:

jshell> 0x98
$1 ==> 152

jshell> (byte)0x98
$2 ==> -104
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • what is similar to unsigned byte in java ? is casting is a good solution ? I think java should handle this internally ? – Sodrul Amin Shaon Mar 18 '21 at 07:34
  • Actually, Java does not have unsigned integral types. I don't think there is anything wrong with casting. It's the way Java is. You can't specify byte literals in the language, so just cast, it's fine. More info here: https://stackoverflow.com/q/5193883/831878 – Ray Toal Mar 18 '21 at 08:27
0

The simplistic answer to this question is that 0x98 is not a byte. Java bytes are signed and range from -128 to +127. 0x98 is greater than +127.

But the real explanation starts with the FACT that Java doesn't have byte literals at all. Every integer literal denotes either an int value or a long value.

So how does this work then?

 byte b = 1;

It works because there is a special rule that allows a compile time constant expression whose type is int to be assigned to a byte if and only if the value is in range of byte. So the above works, but:

byte b2 = 0x98;  // ERROR

is rejected because 0x98 is outside of the range of byte.

And the same rule applies within array initializers.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216