Friday 10 August 2018

android - How can a Java byte variable be 128?

I probably miss some fundamental understanding of byte in Java. The following is a simplified excerpt from an app to illustrate the problem:



public class Foo
{

byte b1;
byte b2;
byte bProblem;
}


foo is an instance of Foo. The following has puzzled me for hours:



Log.d("Debug", "Before: " + String.valueOf(foo.bProblem));
if (foo.bProblem != (byte) 0x80) {

foo.bProblem = (byte) 0x80;
Log.d("Debug", "After: " + String.valueOf(foo.bProblem));
}


LogCat shows the following:



03-17 21:58:46.590: D/Debug(2130): Before: 128    
03-17 21:58:46.590: D/Debug(2130): After: -128



Eclipse's debugger always shows -128 (0x80) for foo.bProblem. This means the debugger cannot see what String.valueOf() reveals. How can a Java byte be 128?



I noticed this when adding foo.bProblem to a List caused:
Java.lang.ArrayIndexOutOfBoundsException: length=256; index=256



Could anyone offer some hint for me to understand this?



Edited:




I found later this happens only on an Intel Android emulator as I wrote in my comment following Joop's answer.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...