java int强制类型转换为String 报错,请各位解释
伊谢尔伦
伊谢尔伦 2017-04-17 17:56:50
0
11
1425

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(11)
巴扎黑

Forced type conversion must have an inheritance or implementation relationship, right? int是基本类型,String是引用类型,两个离的更远了。还有,即使是Integer也和String不存在继承关系,转换为String建议使用String.valueOf().toString().

伊谢尔伦

First of all, the int type is a basic numerical type, not a class type; secondly, Integer inherits from java.lang.Number, so it cannot be cast. The simplest, you can use the following method to convert numeric type to string type:

int x = 1;
String xx=""+x;
PHPzhong

String.valueOf(x); should be used for the most efficiency. Don’t use ""+x"

大家讲道理

int and String are not the same type at all.

Integer x = 1;
String xx = x.toString();
Peter_Zhu

Forced conversion can only occur in inheritance relationships. . Int and String are different types, so naturally they cannot be forced to convert

Ty80

Forced type conversion does not have to have an inheritance or implementation relationship, such as short and int
It can only be said that the int type to the String type cannot be operated in this way
The other issue is the efficiency of the operation

巴扎黑

int and String are different types, use String.valueOf(x);

大家讲道理

String.valueOf(x)

Ty80

Cannot be converted like this, it needs to be like this: String s = String.valueOf(x);

刘奇
String  s = String.valueOf(int);
String  s = String.valueOf(double);
String  s = String.valueOf(boolean);
//...
int     i = Integer.parseInt("1");
double  d = Double.parseDouble("1.1");
boolean b = Boolean.parseBoolean("true");
//...
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!