问题
My Nexus 5X device running Android 7.0 (API level 24) has an extremely weird behavior when printing and comparing the values of short
fields.
public class TestActivity {
public class ShortWrapper {
public short s;
}
public void onCreate(Bundle b) {
// Local short
short s1 = -10;
Log.d(TAG, "Local short (concatenation) = " + s1);
Log.d(TAG, String.format("Local short (String.format) = %d", s1));
if(s1 == -10) Log.d(TAG, "EQUAL"); else Log.d(TAG, "!!! NOT EQUAL !!!");
// Short field
TestActivity.ShortWrapper w = new TestActivity.ShortWrapper();
w.s = -10;
Log.d(TAG, "Short field (concatenation) = " + w.s);
Log.d(TAG, String.format("Short field (String.format) = %d", w.s));
if(w.s == -10) Log.d(TAG, "EQUAL"); else Log.d(TAG, "!!! NOT EQUAL !!!");
}
}
Output:
Local short (concatenation) = -10
Local short (String.format) = -10
EQUAL
Short field (concatenation) = 65526 <<<<<<<<< ???
Short field (String.format) = -10
!!! NOT EQUAL !!! <<<<<<<<< ???
On another Android device running Android 4.4, and on an Android 7.0 emulator, the output is different, i.e. as expected.
Why is the Nexus 5X dealing so weirdly with this negative short field? Is this a bug of the platform or device ?
来源:https://stackoverflow.com/questions/40104963/negative-short-primitive-has-wrong-value-on-android-24