包装类&数学类&日期类
1 包装类
把基本数据类型包装成引用数据类型
byte | short | int | long | float | double | char | boolean | void |
---|---|---|---|---|---|---|---|---|
Byte | Short | Integer | Long | Float | Double | Character | Boolean | Void |
Void类
public static Void demo1(){ return null; }
Number类的数值类型的子类
Byte,Short,Integer,Long,Float,Double
自动装箱: 自动把基本数据类型转换成引用数据类型
自动装箱底层调用了valueOf()方法
对于Byte来说,在-128到127范围内,返回的是同一个对象
对于Short,Integer,Long来说,在-128到127范围内,返回的是同一个对象,超过这个范围就每次返回新的对象
对于Float和Double来说,永远返回新的对象
对于Character来说,0-127返回的是同一个对象,超出范围返回的是新对象
对于Boolean来说,true就返回TRUE对象,false就返回FALSE对象
// 装箱: 把基本数据类型包装成引用数据类型 Integer integer = new Integer(10); System.out.println(integer); // JDK5特性:自动装箱: 自动会把基本数据类型包装成引用数据类型 Integer integer1 = 10; System.out.println(integer1); Double dou = 3.14; System.out.println(dou); Float f = 3.55F; System.out.println(f); // 自动装箱底层调用了valueOf()方法 Integer in1 = 1000; Integer in2 = 1000; System.out.println(in1 == in2);
自动拆箱:自动把引用数据类型转换成对应的基本数据类型
// 拆箱:把引用数据类型转换成对应的基本数据类型 Integer integer = new Integer(10); int i = integer.intValue(); System.out.println(i); //JDK5特性: 自动拆箱:自动把引用数据类型转换成对应的基本数据类型 // 自动拆箱调用的是xxxValue方法 int j = integer; //当引用数据类型和对应的基本数据类型进行运算的时候,会自动拆箱 Integer in = 1000; int x = 1000; System.out.println(in == x); if (in > 100){ System.out.println("自动拆箱了"); } Double dou = 3.14; double d1 = dou.doubleValue(); Long l = 10L; long l1 = l.longValue(); // 这个构造方法中的符号只能是 + - Integer integer1 = new Integer("123"); System.out.println(integer1 == 123); // 把字符串转换成对应的基本数据类型 int parseInt = Integer.parseInt("345"); // Byte.parseByte() // Short.parseShort() // Long.parseLong() // Float.parseFloat(); // Double.parseDouble() // 底层是用 "true" 和传入的字符串进行忽略大小写的比较 boolean aaa = Boolean.parseBoolean("TRue"); System.out.println(aaa); // 字符串不能直接转字符 char c = "abc".charAt(1);
包装类产生的对象的哈希码值是固定不变的
对于Byte,Short,Integer,Long来说,返回的都是数值本身
Float和Double也是固定值
Character返回的字符是int类型,是对应的码表值
Boolean如果是true返回的是1231,如果是false返回的是1237
// 包装类产生的对象的哈希码值是固定的 System.out.println(in.hashCode()); System.out.println(Byte.hashCode((byte)10)); System.out.println(Short.hashCode((short) 100)); System.out.println(Long.hashCode(1000L)); System.out.println(Float.hashCode(3.14F)); System.out.println(Character.hashCode('a')); System.out.println(Boolean.hashCode(true)); // NaN : not a number 非数字 和自身都不相等 double a = 0.0 / 0; double b = 0.0 / 0; System.out.println(a == b); // 判断是否是非数字 System.out.println(Double.isNaN(a)); double c1 = 1.0 / 0; System.out.println(c1); // 判断数字是否是正无穷 System.out.println(Double.isInfinite(1.0 / 0)); // 把十进制转换成二进制 String binaryString = Integer.toBinaryString(10); System.out.println(binaryString); // 把十进制转换成八进制 String octalString = Integer.toOctalString(8); System.out.println(octalString); // 把十进制转换成十六进制 String hexString = Integer.toHexString(16); System.out.println(hexString); System.out.println(new Object());
2 数学类
2.1 Math类
Math是一个最终类,并且构造方法是私有的,所有的属性和方法都是被static修饰的,可以通过类名来调用
public static void main(String[] args) { // 自然指数 System.out.println(Math.E); // 圆周率 System.out.println(Math.PI); // 绝对值 System.out.println(Math.abs(-10)); // 立方根 System.out.println(Math.cbrt(27)); // 向上取整 System.out.println(Math.ceil(3.1)); // 向下取整 System.out.println(Math.floor(-2.1)); // 求最大值 System.out.println(Math.max(10,20)); // 求最小值 System.out.println(Math.min(20,10)); // 加权函数 a的b次方 System.out.println(Math.pow(2,3)); // 四舍五入 System.out.println(Math.round(3.5)); // 平方根 System.out.println(Math.sqrt(16)); }
2.2 BigDecimal
用于精确计算的类,要求参数以字符串形式传入,底层是做的按位运算
public static void main(String[] args) { BigDecimal bigDecimal1 = new BigDecimal("10.0"); BigDecimal bigDecimal2 = new BigDecimal("3.0"); // 加法 System.out.println(bigDecimal1.add(bigDecimal2)); // 减法 BigDecimal subtract = bigDecimal1.subtract(bigDecimal2); System.out.println(subtract); // 转换成double类型 double v = subtract.doubleValue(); // 乘法 System.out.println(bigDecimal1.multiply(bigDecimal2)); // 除法 System.out.println(bigDecimal1.divide(bigDecimal2, RoundingMode.UP)); }
2.3 BigInteger
用于大量数字运算的类,最多能到67,108,864位
public static void main(String[] args) { BigInteger bigInteger1 = new BigInteger("435465464354235464564325467547"); BigInteger bigInteger2 = new BigInteger("43546546435423546456432546754"); System.out.println(bigInteger1.multiply(bigInteger2)); }
面试题: 有两个千位的数字相乘,你能提供几种方案?
第一种方案:BigInteger
第二种方案
public static void main(String[] args) { // 两个千位数字相乘 // 定义两个数组,分别表示两个千位数字 int[] arr1 = {9,9,9,8}; int[] arr2 = {4,6,7,8}; // 定义数组,表示结果 int[] result = new int[arr1.length + arr2.length]; for (int i = 0; i < arr1.length; i++) { for (int j = 0; j < arr2.length; j++) { result[i + j] += arr1[i] * arr2[j]; } } // 处理进位问题 for (int i = 0; i < result.length - 1; i++) { int temp = result[i]; result[i] = temp % 10; result[i + 1] += temp / 10; } System.out.println(Arrays.toString(result)); }
2.4 DecimalFormat
public static void main(String[] args) { double d = 12.99 * 0.9; System.out.println(d); // 0代表占位,表示一位数字。如果没有数字,用0代替 DecimalFormat decimalFormat = new DecimalFormat("00.00"); System.out.println(decimalFormat.format(11.694)); // #代表占位,表示一位数字,如果这一位没有数字,就不填充 DecimalFormat decimalFormat1 = new DecimalFormat("#0.00"); System.out.println(decimalFormat1.format(0.298)); // 科学计数法 double d1 = 5756756565000L; DecimalFormat decimalFormat2 = new DecimalFormat("0.00000000E0"); System.out.println(decimalFormat2.format(d1)); }
3 日期日历类
3.1 Date类和SimpleDateFormat类
public static void main(String[] args) throws ParseException { // 获取当前系统的时间 Date date = new Date(); System.out.println(date); // 获取date对象到计算机元年的时间 long time = date.getTime(); System.out.println(time); Date date1 = new Date(1756887599280L); System.out.println(date1); // 获取当前时间距离计算机元年的毫秒值 long l = System.currentTimeMillis(); Date date2 = new Date(); // 创建日期格式化对象 默认格式: yyyy/M/d 上下午时:分 // 2025年09月03日 16:25:33.456 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss.SSS a EE"); // 把日期转换成字符串 String format = simpleDateFormat.format(date2); System.out.println(format); // 把字符串转换成日期 Date parse = simpleDateFormat.parse(format); System.out.println(parse); }
3.2 Calendar类
public static void main(String[] args) { // 获取日历类 Calendar calendar = Calendar.getInstance(); // 年 int year = calendar.get(Calendar.YEAR); System.out.println(year); // 月 int month = calendar.get(Calendar.MONTH); System.out.println(month + 1); // 日 int day = calendar.get(Calendar.DAY_OF_MONTH); System.out.println(day); // 周几 周日是第一天 int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); System.out.println(dayOfWeek - 1); // 一年的第几天 int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR); System.out.println(dayOfYear); // 这一天的第几个小时 int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); System.out.println(hourOfDay); // 分 int minute = calendar.get(Calendar.MINUTE); System.out.println(minute); // 秒 int second = calendar.get(Calendar.SECOND); System.out.println(second); // 毫秒 int millisecond = calendar.get(Calendar.MILLISECOND); System.out.println(millisecond); // 设置年月日时分秒 calendar.set(2020,9,1,18,18,44); System.out.println(calendar.get(Calendar.YEAR)); // 设置日期 calendar.setTime(new Date()); }
课堂练习:
每个月的第三周的周六进行交易。用户输入一个字符串,告诉用户交易是否开始并提示:交易未开始,交易正在进行中,交易已结束...
private static void practice() throws ParseException { String str = new Scanner(System.in).next(); // 把字符串转换成日期 // 创建日期格式化对象 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 把用户输入的字符串转换成日期对象 Date date = simpleDateFormat.parse(str); // 获取日历对象 Calendar calendar = Calendar.getInstance(); // 设置日历的时间 calendar.setTime(date); // 获取第几周 int week = calendar.get(Calendar.WEEK_OF_MONTH); // 获取周几 int day = calendar.get(Calendar.DAY_OF_WEEK); if (week == 3 && day == 7){ System.out.println("交易正在进行中"); }else if (week < 3 || week == 3 && day < 7){ System.out.println("交易还未开始"); }else { System.out.println("交易已结束"); } }
3.3 java.time包
在jdk1.8中,对时间体系进行了新的划分,将日期和时间以及其他的信息进行分割,从而分出来一个代表时间的包--java.time
3.3.1 LocalDate
只有日期没有时间
private static void demo1() { // 日期 // 获取当前时间 LocalDate localDate = LocalDate.now(); System.out.println(localDate); // 指定年月日 LocalDate localDate1 = LocalDate.of(2002, 3, 5); System.out.println(localDate1); // 添加日期 localDate1 = localDate1.plus(3, ChronoUnit.YEARS); System.out.println(localDate1); // 减少日期 localDate1 = localDate1.minus(5,ChronoUnit.MONTHS); System.out.println(localDate1); }
3.3.2 LocalTime
只有时间,没有日期
// 只有时间,没有日期 精确到纳秒 LocalTime localTime = LocalTime.now(); System.out.println(localTime); // 指定时间 LocalTime localTime1 = LocalTime.of(12, 10, 1); System.out.println(localTime1); // 添加时间 localTime1 = localTime1.plus(3,ChronoUnit.HALF_DAYS); System.out.println(localTime1); // 减少时间 localTime1 = localTime1.minus(6,ChronoUnit.SECONDS); System.out.println(localTime1);
3.3.3LocalDateTime
private static void demo4() { LocalDateTime date1 = LocalDateTime.of(2022,10,5,10,10,10); // 获取格式化对象 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"); // 把日期时间转换成字符串 String str = dateTimeFormatter.format(date1); System.out.println(str); // 把字符串转换成日期时间 LocalDateTime parse = LocalDateTime.parse(str, dateTimeFormatter); System.out.println(parse); } private static void demo3() { // 有日期 有时间 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime); // 指定日期和时间 LocalDateTime localDateTime1 = LocalDateTime.of(2029, 10, 1, 9, 0, 0); System.out.println(localDateTime1); // 添加时间 localDateTime1 = localDateTime1.plus(10,ChronoUnit.YEARS); System.out.println(localDateTime1); // 减少时间 localDateTime1 = localDateTime1.minus(5,ChronoUnit.MONTHS); System.out.println(localDateTime1); }