short a=3; short b=4; short c=a+b;//报错 short d=3+4;//没错 这是为什
来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:数学作业 时间:2024/07/10 11:40:19
short a=3; short b=4; short c=a+b;//报错 short d=3+4;//没错 这是为什么?
同样是3+4,为什么用变量相加就会报错,而直接用数字相加就没错呢?
同样是3+4,为什么用变量相加就会报错,而直接用数字相加就没错呢?
![short a=3; short b=4; short c=a+b;//报错 short d=3+4;//没错 这是为什](/uploads/image/z/8830715-59-5.jpg?t=short+a%3D3%3B+short+b%3D4%3B+short+c%3Da%2Bb%3B%2F%2F%E6%8A%A5%E9%94%99+short+d%3D3%2B4%3B%2F%2F%E6%B2%A1%E9%94%99+%E8%BF%99%E6%98%AF%E4%B8%BA%E4%BB%80)
我来为你解答这个问题,java中只有int的值超出范围时,才会从头开始计数,就比如 int i=2147483647时,是int的最大值.
;i=i+1;这时int就会超出范围,它就会从-2147483648开始,从最小的又开.但是其他类型不是这样的,就像short类型它默认不是这样的,它如果超出范围就会报错.你这里的short c=a+b,因为a是变量,b是变量,它不能确定变量是不是超出short的范围,就报错了,short d=3+4是两个具体的数值相加,它能判断出3和4都在short的范围内,自然不会报错.
再问: short c=a+b;他们在本质上也是3+4,java在处理他们的时候与直接3+4有什么不同吗?
;i=i+1;这时int就会超出范围,它就会从-2147483648开始,从最小的又开.但是其他类型不是这样的,就像short类型它默认不是这样的,它如果超出范围就会报错.你这里的short c=a+b,因为a是变量,b是变量,它不能确定变量是不是超出short的范围,就报错了,short d=3+4是两个具体的数值相加,它能判断出3和4都在short的范围内,自然不会报错.
再问: short c=a+b;他们在本质上也是3+4,java在处理他们的时候与直接3+4有什么不同吗?
short a=3; short b=4; short c=a+b;//报错 short d=3+4;//没错 这是为什
He is a ______man.A short neck B short -neck C short-necked
a short
short
The girl is shadow is ____at noon.A short B long C not short
-下列变量定义中合法的是( ) A.short a=1-0.1e-1 B.double b=1+5e2.5 C.long
A:Is this tree short?B:No______________________________.
She is ( )short and thin.A an B a C the D some
I am myself short of( ) a,a change b,changes c,change d,the
I 'll take a short ____by bike.A.travel B.trip C.distance D.
We can call “Do It Yourself”DIY( )short A.at B.in C.for D.wi
:sally is tall ___ short hair.A.with B.of C.for D.in