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);会错?
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);会错?
s=s+1相当于short的自增一,是在寄存器中直接加,所以不用强转2.
你看一下getComponentType方法的doc,你直接对String.class调用,其返回值是null,当然出错了
Returns the <code>Class</code> representing the component type of an
array. If this class does not represent an array class this method
returns null.
但是s=s+1,(s+1)是Int,要显式转化为short