I get a strange problem on some android phones, when I run the code below.

    public void test(){
        int num = 1;
        for(int i=0;i<14;i++){
            num = num+100000;
        }
        System.out.println("num  "+num);
    }

I expect the answer "1400001", but I get the wrong answer "200385" on the 
phone like MotoX 2ndGen, Xiaomi3, Xiaomi note etc.

The strangest thing is that I can get the right answer when I want to debug 
it, or complicate the code like

    public void test(){
            int num = 1;
            for(int i=0;i<14;i++){
                num = num+100000;
                System.out.println("num  "+num);
            }
        } 

or

    public void test(){
            int num1 = 1;
            for(int i=0;i<14;i++){
                num1 = num1+100000;
            }
            System.out.println("num1  "+num1);
            int num2 = 1;
            for(int i=0;i<14;i++){
                num2 = num2+100000;
            }
            System.out.println("num2  "+num2);
        }

or turn `int` to `long`, or make "100000" to 100, it can get the right 
answer usually. So what is the reason behind this phenomenon? An integer 
overflow maybe?

supplement:
I test  Integer.SIZE 
<https://developer.android.com/reference/java/lang/Integer.html#SIZE> on 
the wrong phones is 32 , and the Integer.MAX_VALUE 
<https://developer.android.com/reference/java/lang/Integer.html#MAX_VALUE> is 
2147483647,as others.And I find that one of results would overflow on 
"131072" ,changing to binary is 2^17.And all of the wrong result occur on 
fourfold times in "for" code,like the fourth , the eighth etc.so why?

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/d5ba839d-d711-429f-9acb-7fc92437788c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to