search

Home  >  Q&A  >  body text

java int强制类型转换为String 报错,请各位解释

伊谢尔伦伊谢尔伦2766 days ago1451

reply all(11)I'll reply

  • 巴扎黑

    巴扎黑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().

    reply
    0
  • 伊谢尔伦

    伊谢尔伦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;

    reply
    0
  • PHPz

    PHPz2017-04-17 17:58:50

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:58:50

    int and String are not the same type at all.

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

    reply
    0
  • 天蓬老师

    天蓬老师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

    reply
    0
  • PHP中文网

    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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:58:50

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:58:50

    String.valueOf(x)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:58:50

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

    reply
    0
  • 怪我咯

    怪我咯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");
    //...

    reply
    0
  • Cancelreply