Java學習記錄7 — 最大最小數

張小雄
Dec 30, 2020

--

找出int值裡的最大跟最小數值

好玩的試看看

最大數 +1

跟最小數 -1

會打印出什麼

小挑戰:

若賦予變量低於-2147483648跟大於2147483647的值會如何?

延伸閱讀:為啥最大跟最小數值是這個數

These are maximum and minimum int values because the int value is store in 32 bits of space( which is 4 bytes).

This corresponds to a total of 2³² different combinations.

These 32 bits can have various binary values (i.e. 0 or 1) which collectively represent an integer.

So in theory we can make 4294967296 different characters using 32 bits.

In java, we decided to split it into 2 halves(for positive and negative values) so there are -2147483648 negative values and 2147483647 positive values.

You might wonder why we have 2147483647 positive values (i.e. one less than that of negative), that is because one value is given to zero.

You can refer oracle docs and know more — https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

by Rudransh

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet