初学者基础问题,我的ID就是立刻结贴的保障! 为什么short i = 10; 没有错而float a = 1.0;却有错。 (不要回答我将1.0改成1.0F)
整数默认全当做int来处理,浮点数默认全当做double来处理,是这样的吧? 如果是这样的话,那为什么 short i = 10;没有错,10不应该是int类型的么? 如果说10没有超过short的范围,所以可以这么做,那为什么float a = 1.0;又有错了?1.0也没有超过float的范围啊?
float m=1.0;//它会提示:Type mismatch: cannot convert from double to float float m=1.0f;//这个显示指定了使用单精度,当然没问题。
对于整数而言,它默认的确是int类型,但是如果没有超出当前类型大小则会默认进行转换。 我们从下面可以看出默认的是int类型。 对于long long mm=9999999999;//The literal 9999999999of type int is out of range 这里对于Int来说是超出范围了 long mm=9999999999L// 这个却没有错误,说明默认就是Int类型的。
byte m=1;//没有问题吧,根据当前是byte来处理。 byte m=444;//明显就会提示:Type mismatch: cannot convert from int to byte
可见范围超出之后它默认使用Int来处理了。
short m=1;//没问题 short m=1111111111;//Type mismatch: cannot convert from int to short