Android 24上的负短基元有错误的价值

时间:2016-10-18 09:49:49

标签: java android short

运行Android 7.0(API级别24)的我的Nexus 5X设备在打印和比较short字段的值时会出现非常奇怪的行为。

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 !!!");
    }
}

输出:

Local short (concatenation) = -10
Local short (String.format) = -10
EQUAL
Short field (concatenation) = 65526         <<<<<<<<< ???
Short field (String.format) = -10
!!! NOT EQUAL !!!                           <<<<<<<<< ???

在运行Android 4.4的另一台Android设备上,以及在Android 7.0模拟器上,输出不同,即按预期进行。

为什么Nexus 5X对这个负面短场处理如此奇怪? 这是平台或设备的错误吗?

0 个答案:

没有答案