Java中Integer的最大值和最小值

从JDK1.0开始,Integer中就定义了MIN_VALUE和MAX-VALUE两个常量:
/**
*Aconstantholdingtheminimumvaluean{@codeint}can
*have,-231.
*/
publicstaticfinalintMIN_VALUE=0x80000000;
/**
*Aconstantholdingthemaximumvaluean{@codeint}can
*have,231-1.
*/
publicstaticfinalintMAX_VALUE=0x7fffffff;
Q1:谁能给解释一下,这两个常量为什么会分别定义成0x80000000和0x7fffffff。
Q2:java.lang.String的最大长度是多少?
Q3:如下代码能抛出异常吗?为什么
intx=Integer.MAX_VALUE+10;
if(x>=Integer.MAX_VALUE||x<=Integer.MIN_VALUE){//throwexception}
慕田峪7331174
浏览 1220回答 2
2回答

慕慕森

Q1:四字节的整形有符号是-2^31~2^31-1(MIN_VALUE和MAX-VALUE的注释里也写了),查下对应边界的补码二进制表示就清楚了Q2:java用的不多,不太清楚,应该可以很长,附上String的关键字段/**Thevalueisusedforcharacterstorage.*/privatefinalcharvalue[];/**Theoffsetisthefirstindexofthestoragethatisused.*/privatefinalintoffset;/**ThecountisthenumberofcharactersintheString.*/privatefinalintcount;/**Cachethehashcodeforthestring*/privateinthash;//Defaultto0Q3:intx=Integer.MAX_VALUE+10;x溢出了,x实际为-2^31+9,if条件不成立,不会抛出异常。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript