JavaSE学习笔记 Day15

JavaSE学习笔记 Day15

个人整理非商业用途,欢迎探讨与指正!!
« 上一篇



···

13.2包装类

对基本数据类型进行包装的类。
8个包装类是对基本数据类型进行对象化

13.2.1包装类种类
基本类型包装类型
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

包装类之间是没有转换关系的

public class Demo01 {

	public static void main(String[] args) {
		Byte b = 1;
		Short s = 1;
		Integer i = 1;
		Long l = 1L;//Long的赋值必须添加L结尾
		Float f = 10.0f;
		Double d = 10.0;
		Character c = 'h';
		Boolean boo = false;
//		包装类之间是没有转换关系的
//		s = b;
	}
}
13.2.2拆箱和装箱

装箱:基本类型转换为包装类的过程
拆箱:包装类转换为基本类型的过程

public class Demo02 {

	public static void main(String[] args) {
//		JDK1.5之前
		byte b = 10;
//		装箱
		Byte b1 = Byte.valueOf(b);
		System.out.println(b1);
		System.out.println(b1 instanceof Object);
//		拆箱
		byte b2 = b1.byteValue();
		System.out.println(b2);
		
//		JDK1.5之后(语法简单了很多)
		int i = 10;
//		装箱
		Integer i1 = i;
		System.out.println(i1);
//		拆箱
		int i2 = i1;
		System.out.println(i2);
	}
}
13.2.3Number类

是Byte Short Integer Long Float Double的父类
该类中提供了一组方法,xxxValue可以将包装类转换为指定的基本数据类型

public class Demo03 {

	public static void main(String[] args) {
		Double i = 10.0;
//		数值型的六个包装类都有如下的方法
		byte b = i.byteValue();
		short s = i.shortValue();
		int ii = i.intValue();//拆箱的一种形式
		long l = i.longValue();
		float f = i.floatValue();
		double d = i.doubleValue();
	}
}
13.2.4常用的包装类

Integer Double

13.2.4.1常用的属性
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Double.MIN_VALUE);
13.2.4.2包装类的构造方法
public class Demo05 {
//	从前端获取到的值都是String类型的
	public static void main(String[] args) {
//		创建Integer
		Integer i1 = new Integer(10);
		Integer i2 = 10;//装箱
//		将String转换为基本类型
		Integer i3 = new Integer("10");
		int i = i3;
		//将字符串转换为基本类型
		int a = new Integer("10");
//		非数值型转换为包装类会抛出NumberFormatException数字转换异常 运行时异常
		Integer i4 = new Integer("ABC");
		
//		Double同上
		
	}
}
13.2.4.3包装类的方法
public class Demo06 {
//	将字符串转换为基本类型
	public static void main(String[] args) {
//		方式1:使用最多
		int i1 = Integer.parseInt("123");
		System.out.println(i1 + 100);
		System.out.println("123" + 100);
		
//		方式2:
		int i2 = new Integer("123");
		System.out.println(i2);
		
//		方式3:
		int i3 = Integer.valueOf("123");
		System.out.println(i3);
		
//		double同理
		double d1 = Double.parseDouble("10.02");
		System.out.println(d1);
		
		double d2 = new Double("10.02");
		System.out.println(d2);
		
		double d3 = Double.valueOf("10.02");
		System.out.println(d3);
		
		System.out.println(Integer.min(10, 20));
		System.out.println(Integer.max(10, 20));
	}
}
13.2.4.4包装类缓冲区

整数类型的包装类,在范围-128~127之间赋值时是存储在方法区的缓冲区中
在范围-128~127,自动装箱时是从缓冲区中拿值
若不在该范围之内,都是重新new堆中地址
缓冲区是自动装箱时限定的

public class Demo07 {

	public static void main(String[] args) {
//		自动装箱时会使用缓冲区
		Integer i1 = 10;
		Integer i2 = 10;
//		包装类中的equals都是重写过的
		System.out.println(i1.equals(i2));//比较内容的
//		== :1.基本类型的值比较 2.引用类型的地址比较
//		比较地址 在-128~127 是同一个地址(方法区中的缓冲区内),在该范围内中一个值只有一个(底层为了提交效率使用的)
		System.out.println(i1 == i2);
		
//		超过范围了
		i1 = 128;//i1 = new Integer(128);
		i2 = 128;//i2 = new Integer(128);
		System.out.println(i1.equals(i2));//比较内容
//		超过范围都是new新的地址
		System.out.println(i1 == i2);
		
//		比较new时的内容和地址 一个new一个堆地址(new的地址和范围就没有关系了)
		i1 = new Integer(10);
		i2 = new Integer(10);
		System.out.println(i1.equals(i2));
		System.out.println(i1 == i2);
	}
}

13.3java.util.Date类型

表示一个特定的日期,精确到毫秒

public class Demo01 {

	public static void main(String[] args) {
//		创建当前的日期
		Date d = new Date();
//		Mon Dec 11 15:16:10 CST 2023 toString已经重写了
		System.out.println(d);
/**
 * 使用构造创建对象
 * 	year:年份 从1900年开始
 * 	month:月份 0~11表示1~12月
 * 当前构造表示以过时方法,不建议调用
 * */
		Date d2 = new Date(2023 - 1900,12 - 1,11);
		System.out.println(d2);
//		获取毫秒数,具体1900年01月01日08:00:00的毫秒数(有时区)
		System.out.println(d2.getTime());
	}
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值