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:
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
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");
//...
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:
String.valueOf(x); should be used for the most efficiency. Don’t use ""+x"
int and String are not the same type at all.
Forced conversion can only occur in inheritance relationships. . Int and String are different types, so naturally they cannot be forced to convert
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)
Cannot be converted like this, it needs to be like this: String s = String.valueOf(x);