String --> int

第一种方法:int i = Integer.parseInt(s);

第二种方法:int i = Integer.valueOf(s).intValue();

两种方法的区别:Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),所以第二种方法会产生多余的一个对象,而第一种方法不会。

 

int --> String

第一种方法:String s = i + "";

第二种方法:String s = String.valueOf(i);

第三种方法:String s = Integer.toString(i);

三种方法的区别:第一种方法会产生两个String对象,而第二种和第三种方法只会产生一个String对象。

相关文章:

  • 2022-12-23
  • 2022-02-27
  • 2021-12-26
  • 2021-12-05
猜你喜欢
  • 2021-12-24
  • 2022-12-23
  • 2021-12-08
  • 2021-12-06
  • 2021-12-07
  • 2021-06-30
  • 2022-12-23
相关资源
相似解决方案