Negative short primitive has wrong value on Android 24

孤者浪人 提交于 2019-12-10 23:08:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!