测试包装类

包装类和自动装箱和拆箱
自动类型转换

class Cast{
	/*自动类型转换:容量小的数据类型可以自动转换为容量大的数据类型
	 *short:二字节数据 	-32738~32737
	 */
	int x=300;
	short y=32767;
	/*强制类型转换:精度降低或是益处还有丢失信息
	 * 
	 */
	int z=(int)3.14;//z值为3
	byte w=(byte)x;//不同类型强转,超出范围是不同的值,300-44
	long q=1111111111111L;
	public static void cal() {
		int x=(int) Math.pow(10, 9);
		int y=(int) Math.pow(10, 3);
		System.out.println(x);
		System.out.println(y);
		int z=x*y;
		long w=x*y;
		long q=x*(long)y;
		System.out.println(x);
		System.out.println(z+"---"+w+"---"+q);
	}
}

测试包装类

class BaoZhuang{
	public static void castt() {
//	基本数据类型转为包装类对象
		Integer a=new Integer(3);  	//可以写成 integer a=3;自动装箱
		Integer b=Integer.valueOf(3);
//	包装类对象转为基本数据类型
		int c=b.intValue();
		double d=b.doubleValue();
//	字符串转为包装类对象
		Integer e=new Integer("999");
		Integer f=Integer.parseInt("46546");
//	包装类对象转为字符串
		String g=e.toString();
		String t=f+"";
//	常量
		int x=Integer.MAX_VALUE;
		System.out.println(x);		//2147483647
	}
自动装箱和拆箱
	public static void zhuangchai() {
//		空指针异常,对象为空,调用其方法
		Integer a=123;	//自动装箱 integer a=integer.valueOf();
		int b=a;		//自动拆箱 int b=integer.intValue();
//		自动缓存-128~127之间的数字吗,到常量池中
		Integer i1=123;
		Integer i2=123;
		System.out.println(i1==i2);
		System.out.println(i1.equals(i2));
		
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值