java中的那些数据类型可以进行强制转换?String和int如何进行强制转换?

如题所述

int转String:
int n = 5;
String s1 = n+"";
String s2 = Integer.toString(n);
String s3 = String.valueOf(n);
String转int:
String s = "3";
int n=Integer.parseInt(s);因为经常有字符串不全为数字,所以最好try catch一下
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-04-22
将 String类型强制转换成int类型
方法一:int i=System.Convert.ToInt32(String)
方法二:
try{
int i=Int32.Parse(String);
}
catch{
}
有一些并不一定能够转换的,加一个try

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网