Sunday, February 24, 2019

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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...