Integer与int和String的转换

int转为Integer

public static void main(String[] args) {
          
   
				int num=9;
				Integer numIngeter=new Integer(num);
				Integer numIngete2=Integer.valueOf(num);
				System.out.println(numIngeter);
				System.out.println(numIngete2);
	}

Integer 转为int

public static void main(String[] args) {
          
   
			Integer integer=new Integer(666);
			int i=integer.intValue();
			System.out.println(i);
	}

字符串转 Integer

public static void main(String[] args) {
          
   
		String string=new String("sa");
		
		String string2=new String("666");
		
		Integer integer2=Integer.valueOf(string2);//只能转数字类型
//		Integer integer=Integer.valueOf(string);//很明显报错(NumberFormatException)数值转换异常
		System.out.println(integer2);
	}

字符串转int

String string=new String("5adkk");
		String string1=new String("56666");
//		int num=Integer.parseInt(string);//很明显报错(NumberFormatException)数值转换异常
		int num2=Integer.parseInt(string1);
		
//		System.out.println(num);
		System.out.println(num2);
经验分享 程序员 微信小程序 职场和发展