JAVA类型直接输入超过10位报The literal XXX of type int is out of range 解决办法

Long.parseLong("XXX")long number=xxxL

java long与int取值范围的问题

1:long l = 1024*1024*1024*4; //0
2:int i1 = 1024*1024*1024*4; //0
3:int i2 = 220000000000; //错误
4:long l1 = 220000000000; //错误
5:long l1 = 220000000000L; //正确

1-4都是超过int(-2^31 — 2^31-1)的范围   

如果不加L在后面标明 默认是int型 所以4是错误的 5是正确的
3明显溢出编译时就判断错已经超出范围
1与2主要涉及到乘法的问题
java在相乘之前不知道你的结果是否溢出
只有在后并赋值的时候才知道超出了int类型和long类型的范围,应该是后台自己处理超出范围的数导致的结果

当前int型数据达到最大值2147483647时,是第一位为0,其余31位为全1,01111111111111111111111111111111;再加1则进1,第一位为1,其余全零,10000000000000000000000000000000。系统把第一位判断为负号,而且同时代表值,所以是-2147483648,当再继续相加时,系统会把之前的数当做负数,再加上正数1,则为-2147483647,10000000000000000000000000000001,后面以此类推。结合测试1,当为10000000000000000000000000000000时,即2147483648,再乘以2将溢出,溢出位娶不到,全0,即为00000000000000000000000000000000,此时为0(-2147483648再乘以2为什么是0的理解) . 再乘以2依然为0.

为什么32位机int取值范围是-2^31~2^31-1?为什么不是-2^31-1~2^31-1?

因为int类型总长度是2^32,所以对于有符号数来说,正负各占一半,也就是你说的2^31,但是由于0被划分到属于正数的范畴(但不是正数)所以正数要比负数少一个数

【最小值:-2^31】为什么不是-(2^31-1)?

有关原码和补码请看:

二进制原码和补码

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
You are visitor as of October 17, 1996.The Art of Assembly Language ProgrammingForward Why Would Anyone Learn This Stuff?1 What's Wrong With Assembly Language2 What's Right With Assembly Language?3 Organization of This Text and Pedagogical Concerns4 Obtaining Program Source Listings and Other Materials in This TextSection One: Machine OrganizationArt of Assembly Language: Chapter OneChapter One - Data Representation1.0 - Chapter Overview1.1 - Numbering Systems1.1.1 - A Review of the Decimal System1.1.2 - The Binary Numbering System1.1.3 - Binary Formats1.2 - Data Organization1.2.1 - Bits1.2.2 - Nibbles1.2.3 - Bytes1.2.4 - Words1.2.5 - Double Words1.3 - The Hexadecimal Numbering System1.4 - Arithmetic Operations on Binary and Hexadecimal Numbers1.5 - Logical Operations on Bits1.6 - Logical Operations on Binary Numbers and Bit Strings1.7 - Signed and Unsigned Numbers1.8 - Sign and Zero Extension1.9 - Shifts and Rotates1.10 - Bit Fields and Packed Data1.11 - The ASCII Character Set1.12 Summary1.13 Laboratory Exercises1.13.1 Installing the Software1.13.2 Data Conversion Exercises1.13.3 Logical Operations Exercises1.13.4 Sign and Zero Extension Exercises1.13.5 Packed Data Exercises1.14 Questions1.15 Programming ProjectsChapter Two - Boolean Algebra2.0 - Chapter Overview2.1 - Boolean Algebra2.2 - Boolean Functions and Truth Tables2.3 - Algebraic Manipulation of Boolean Expressions2.4 - Canonical Forms2.5 - Simplification of Boolean Functions2.6 - What Does This Have To Do With Computers, Anyway?2.6.1 - Correspondence Between Electronic Circuits and Boolean Functions2.6.2 - Combinatorial Circuits2.6.3 - Sequential and Clocked Logic2.7 - Okay, What Does It Have To Do With Programming, Then?2.8 - Generic Boolean Functions2.9 Laboratory Exercises<

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值