当前位置: 首页 > news >正文

JAVA 十二幕啦啦啦啦啦啦啦啊啦啦啦啦a

包装类&数学类&日期类

1 包装类

把基本数据类型包装成引用数据类型

byteshortintlongfloatdoublecharbooleanvoid
ByteShortIntegerLongFloatDoubleCharacterBooleanVoid
  • 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.456SimpleDateFormat 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);}

http://www.dtcms.com/a/364877.html

相关文章:

  • AI IDE+AI 辅助编程,真能让程序员 “告别 996” 吗?
  • 【Java】对于XML文档读取和增删改查操作与JDBC编程的读取和增删改查操作的有感而发
  • 面试题:JVM与G1要点总结
  • 告别 Hadoop,拥抱 StarRocks!政采云数据平台升级之路
  • 海思HI3516CV610-20S,HI3516CV610专为安防市场超高清智慧视觉SoC硬件设计的一款开发板
  • MongoDB 聚合查询超时:索引优化与分片策略的踩坑记录
  • Prometheus监控预警系统深度解析:架构、优劣、成本与竞品
  • CryptMsgGetParam函数分析之CMSG_INNER_CONTENT_TYPE_PARAM
  • 110个作品涨粉210万!用Coze智能体工作流1分钟生成爆款名著金句视频,无需剪辑,附详细教程
  • 【FastDDS】Layer DDS之Domain (01-overview)
  • 限流式保护器+安全用电云平台如何为企业安全用电做双重防护的?
  • 机器学习从入门到精通 - 手撕线性回归与梯度下降:从数学推导到Scikit-Learn实战
  • Scikit-learn Python机器学习 - 特征预处理 - 处理缺失值:SimpleImputer
  • 深度学习与 OpenCV 的深度羁绊:从技术协同到代码实践
  • 苍穹外卖项目实战(日记十四)-记录实战教程及问题的解决方法-(day3课后作业) 菜品停售启售功能
  • centos 压缩命令
  • 解决CentOS 镜像列表服务已下线或迁移导致镜像服务和仓库停止维护解决方案
  • Python:AI开发第一语言的全面剖析
  • Linux之centos 系统常用命令详解(附实战案例)
  • pytorch gpu版本安装(最新保姆级安装教程)
  • 【常用SQL语句和语法总结】
  • Keras/TensorFlow 中 `fit()` 方法参数详细说明
  • leetcode_234 回文链表
  • 如何画时序图、流程图
  • try-catch:异常处理的最佳实践与陷阱规避
  • 2025年互联网行业专业认证发展路径分析
  • RoPE频率缩放机制:解密大语言模型上下文扩展的核心算法
  • 无人机散热模块技术要点分析
  • Diamond基础3:在线逻辑分析仪Reveal的使用
  • 超越马力欧:如何为经典2D平台游戏注入全新灵魂