首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

整型常量与字符串常量相加有关问题

2012-10-13 
整型常量与字符串常量相加问题class DataDemo11{public static void main(String args[]){float f 30.3f

整型常量与字符串常量相加问题
class DataDemo11{
public static void main(String args[]){
float f = 30.3f ;// 浮点型
int x = (int) f;// 强制类型转换
System.out.println("x = " + x) ;
System.out.println("10 / 3 = " + ((float)10 / 3)) ;// 执行强制转换
}
};

结果是:str = lixinghua30
为什么???求解释??

[解决办法]
int x = 30 ;
str = str + x ; // 修改str的内容并将内容重新给str变量
==================================================
因为x与String类型相加时,会默认将x转意成包装类(Integer)
通过Integer的toString方法将x转成String类型
此时str与"30"相加得出你的结果。

热点排行
Bad Request.