4.22基本类型与String之间的转换

拆箱与装箱

/**
 * 基本数据类型都有一个对应的包装类型,为了可以基本类型与String类型相互转换
 * 	byte				Byte
 *	short				Short
 *	int					Integer	
 *	long				Long
 *	float				Float
 *	double				Double
 *	char				character
 *	boolean				Boolean
 * 在JDK1.5之后,Java增加了自动装箱与自动拆箱功能;
 * 包装类型就像是基本类型与String类型之间的中转
 * 基本类型<------>包装类型<------>String
 * */
public class Text1 {
	public static void main(String[] args) {
		int num = 1123;
		int num2 = 2134;
		Integer i = new Integer(num);//装箱
		String str = i.toString();//拆箱
		System.out.println(str);
		String str2 = Integer.toString(num2);//自动装箱
		System.out.println(str2);
		
		String str3 = "1132";
		String str4 = "12321";
		Integer num3 = new Integer(str3);
		int num33 = num3.intValue();
		System.out.println(num33);
		int num44 = Integer.parseInt(str4);
		System.out.println(num44);
	}
}

Integer类

/**
 * Integer类型
 * Integer类型时int类型的包装类
 * 要把一个int类型数据装换为String类型,需要经过Integer类型的中转,进行拆箱与装箱,在JDK1.5之后Java实现了自动拆箱与装箱功能
 * Integer提供了静态功能可以将数据转换为二进制,八进制,十六进制:
 * 			public static String toBinaryString(int i)
 *			public static String toOctalString(int i)
 *			public static String toHexString(int i)
 *构造方法:
 *			public Integer(int num)
 *			public Integer(String str)
 * */
public class Text2 {
	public static void main(String[] args) {
		int i = 111;
		String str = "123";
		//装箱
		Integer i1 = new Integer(i);
		Integer i2 = new Integer(str);
		//拆箱
		String s = i1.toString();
		int ii = i2.intValue();
		System.out.println(s+ii);
	}
}

Character类

/**
 * Character类:
 * Character 类在对象中包装一个基本类型 char 的值。Character 类型的对象包含类型为 char 的单个字段。 
 * 以确定字符的类别(小写字母,数字,等等)
 * 构造方法
 * public Character(char value)
 * 判断功能:
 * public static boolean isDigit(char ch)确定指定字符是否为数字。
 * public static boolean isLowerCase(int ch): 确定是否是小写字母字符
 * public static boolean isUpperCase(int ch):确定是否大写字母字符
 * 
 * 转换功能:
 * public static int toLowerCase(int codePoint)
 * public static int toUpperCase(int codePoint)
 * */
public class Text3 {
	public static void main(String[] args) {
		char ch = 'A';
		Character chh = Character.valueOf(ch);
		String str = chh.toString();
		System.out.println(str);
		System.out.println(chh.isUpperCase(ch));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值