巴扎黑2017-04-17 17:58:50
Forced type conversion must have an inheritance or implementation relationship, right? int
是基本类型,String
是引用类型,两个离的更远了。还有,即使是Integer
也和String
不存在继承关系,转换为String
建议使用String.valueOf()
或.toString()
.
伊谢尔伦2017-04-17 17:58:50
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;
PHPz2017-04-17 17:58:50
String.valueOf(x); should be used for the most efficiency. Don’t use ""+x"
大家讲道理2017-04-17 17:58:50
int and String are not the same type at all.
Integer x = 1;
String xx = x.toString();
天蓬老师2017-04-17 17:58:50
Forced conversion can only occur in inheritance relationships. . Int and String are different types, so naturally they cannot be forced to convert
PHP中文网2017-04-17 17:58:50
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
PHP中文网2017-04-17 17:58:50
Cannot be converted like this, it needs to be like this: String s = String.valueOf(x);
怪我咯2017-04-17 17:58:50
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");
//...