当前位置:   article > 正文

Java 如何将String和Int互相转化_string加int的输出结果

string加int的输出结果

Int----->String,4种方法

public class IntegerDemo2 {
public static void main(String[] args) {
	int num=100;
	//1
	String s1=""+num;
	System.out.println(s1);
	//2
	String s2 =String.valueOf(num);
	System.out.println(s2);
    //3
    Integer i =new Integer(num);
	String s3 =i.toString();
	System.out.println(s3);
	//4
	String s4 =Integer.toString(i);
	System.out.println(s4);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

String---->Int 2种

String s="100";
	Integer ii =new Integer(s);
	int x=ii.intValue();
	System.out.println(x);
	int y = Integer.parseInt(s);
	System.out.println(y);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果
在这里插入图片描述
注:public static int parseInt(String s)
想把字符串转成某一种基本类型,就去找某基本类型的包装类的此方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/835026
推荐阅读
相关标签
  

闽ICP备14008679号