Java类型转换-int-Integer-String

本文详细介绍了Java中int、Integer和String类型之间的转换方法,包括直接转换、使用包装类的静态方法以及toString()方法等。同时提醒开发者注意空字符串判断和类型转换异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java类型转换-int-Integer-String

int与Integer

		// int转Integer:
		int a = 0;
		Integer b = new Integer(a);
		// 或 
		// Integer b = a;
	
		// Integer转int:
		Integer a = new Integer(0);
		int b = a.intValue();

Integer与String

		// Integer转String
		Integer i = 123;
		String string = i.toString();
		
		String string2 = Integer.toString(i);
		
		String string3 = String.valueOf(i);
		
		// String转Integer
		String str = "123";
		Integer integer = Integer.valueOf(str);

int与String

		// String转int
		String s = "123";
		int i1 = Integer.parseInt(s);
		int i2 = Integer.valueOf(s).intValue();
		
		// int转String
		int i = 123;
		String s1 = String.valueOf(i);
		String s2 = Integer.toString(i);
		String s3 = "" + i;

注:需注意类型转换时,字符串是否为空的判断,及类型转换异常(java.lang.ClassCastException)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值