站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2SE 专题问两个简单问题,谢谢

问两个简单问题,谢谢

摘要: 问两个简单问题,谢谢


1,
short s=0;
s=s+1; 有错,1默认是int,从int到short必须强制转换

而s+=1没错,怎样理解?是先把s转为int还是把1转为short? s+=1应该就是s=s+1才对啊。


2,
java.lang.reflect.Array有什么用?
为什么Array.newInstance(java.lang.String.class.getComponentType(),10);会错?


1.s=s+1是short=short+int,所以要强转,是取数相加后再赋值
s=s+1相当于short的自增一,是在寄存器中直接加,所以不用强转

2.
你看一下getComponentType方法的doc,你直接对String.class调用,其返回值是null,当然出错了
Returns the Class representing the component type of an
array. If this class does not represent an array class this method
returns null.


s+=1和s=0的原理一样,都会把int隐式转为short.
但是s=s+1,(s+1)是Int,要显式转化为short


↑返回目录
前一篇: 文件的拷贝怎么实现啊?
后一篇: 关于DOUBLE的问题