+ 2
I think this answer is not right,what do you think?
Int x=0; do{ System.out.println(x); x++; }while(x>0); The max value of int is 2^31,out of this value, the programe will exit a loop
3 ответов
+ 2
The value of x is represented inside the computer as binary (1s and 0s). Like Jamie said, an int is signed by default so there are positive and negative numbers. This is represented in binary by the most significant bit (msb), if the binary number is 1000, the msb is one. If the msb is 0 the number is positive, if the msb is 1 the number is negative. Since int is 32 bits, the computer uses 32 1s and zeros to represent the number. So the largest positvive is a zero at the msb followed by all 1s (0111 1111 1111 1111 1111 1111 1111 1111) which equals 2^31 - 1. If you add one more, because of how computers do arithematic, the most significant bit becomes 1 followed by all zeros (1000 0000 0000 0000 0000 0000 0000 0000). This number is now negative and the loop will exit.
+ 2
The answer in the challenge said this program will in a infinite loop.So I think the answer is wrong