1、昨日复习

  1. 画出如下几行代码的内容结构:
    String s1 = “hello”;
    String s2 = “hello”;
    String s3 = new String(“hello”);
    s1 += “world”;

  2. 如何理解String类的不可变性

  3. String类是否可以被继承?为什么?
    String s = new String(“hello”);在内存中创建了几个对象?请说明

  4. String,StringBuffer,StringBuilder三者的对比

  5. String的常用方法有哪些?(至少7个)
    length() / charAt() / equals() / compareTo() / startsWith() / endsWith()
    containts() / indexOf() / lastIndexOf() / getBytes() / toCharArray() / valueOf() / ….

    2、常见算法题目

    ```java package com.atguigu.java;

import java.util.ArrayList; import java.util.Arrays; import java.util.List;

import org.junit.Test;

/*

  • 1.模拟一个trim方法,去除字符串两端的空格。
  • 2.将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为”abfedcg”
  • 3.获取一个字符串在另一个字符串中出现的次数。 比如:获取“ab”在 “cdabkkcadkabkebfkabkskab”
    中出现的次数

4.获取两个字符串中最大相同子串。比如: str1 = “abcwerthelloyuiodef“;str2 = “cvhellobnm”//10 提示:将短的那个串进行长度依次递减的子串与较长
的串比较。

5.对字符串中字符进行自然顺序排序。”abcwerthelloyuiodef” 提示: 1)字符串变成字符数组。 2)对数组排序,选择,冒泡,Arrays.sort(str.toCharArray()); 3)将排序后的数组变成字符串。

*/ public class StringExer {

  1. // 第1题
  2. public String myTrim(String str) {
  3. if (str != null) {
  4. int start = 0;// 用于记录从前往后首次索引位置不是空格的位置的索引
  5. int end = str.length() - 1;// 用于记录从后往前首次索引位置不是空格的位置的索引
  6. while (start < end && str.charAt(start) == ' ') {
  7. start++;
  8. }
  9. while (start < end && str.charAt(end) == ' ') {
  10. end--;
  11. }
  12. if (str.charAt(start) == ' ') {
  13. return "";
  14. }
  15. return str.substring(start, end + 1);
  16. }
  17. return null;
  18. }
  19. // 第2题
  20. // 方式一:
  21. public String reverse1(String str, int start, int end) {// start:2,end:5
  22. if (str != null) {
  23. // 1.
  24. char[] charArray = str.toCharArray();
  25. // 2.
  26. for (int i = start, j = end; i < j; i++, j--) {
  27. char temp = charArray[i];
  28. charArray[i] = charArray[j];
  29. charArray[j] = temp;
  30. }
  31. // 3.
  32. return new String(charArray);
  33. }
  34. return null;
  35. }
  36. // 方式二:
  37. public String reverse2(String str, int start, int end) {
  38. // 1.
  39. String newStr = str.substring(0, start);// ab
  40. // 2.
  41. for (int i = end; i >= start; i--) {
  42. newStr += str.charAt(i);
  43. } // abfedc
  44. // 3.
  45. newStr += str.substring(end + 1);
  46. return newStr;
  47. }
  48. // 方式三:推荐 (相较于方式二做的改进)
  49. public String reverse3(String str, int start, int end) {// ArrayList list = new ArrayList(80);
  50. // 1.
  51. StringBuffer s = new StringBuffer(str.length());
  52. // 2.
  53. s.append(str.substring(0, start));// ab
  54. // 3.
  55. for (int i = end; i >= start; i--) {
  56. s.append(str.charAt(i));
  57. }
  58. // 4.
  59. s.append(str.substring(end + 1));
  60. // 5.
  61. return s.toString();
  62. }
  63. @Test
  64. public void testReverse() {
  65. String str = "abcdefg";
  66. String str1 = reverse3(str, 2, 5);
  67. System.out.println(str1);// abfedcg
  68. }
  69. // 第3题
  70. // 判断str2在str1中出现的次数
  71. public int getCount(String mainStr, String subStr) {
  72. if (mainStr.length() >= subStr.length()) {
  73. int count = 0;
  74. int index = 0;
  75. // while((index = mainStr.indexOf(subStr)) != -1){
  76. // count++;
  77. // mainStr = mainStr.substring(index + subStr.length());
  78. // }
  79. // 改进:
  80. while ((index = mainStr.indexOf(subStr, index)) != -1) {
  81. index += subStr.length();
  82. count++;
  83. }
  84. return count;
  85. } else {
  86. return 0;
  87. }
  88. }
  89. @Test
  90. public void testGetCount() {
  91. String str1 = "cdabkkcadkabkebfkabkskab";
  92. String str2 = "ab";
  93. int count = getCount(str1, str2);
  94. System.out.println(count);
  95. }
  96. @Test
  97. public void testMyTrim() {
  98. String str = " a ";
  99. // str = " ";
  100. String newStr = myTrim(str);
  101. System.out.println("---" + newStr + "---");
  102. }
  103. // 第4题
  104. // 如果只存在一个最大长度的相同子串
  105. public String getMaxSameSubString(String str1, String str2) {
  106. if (str1 != null && str2 != null) {
  107. String maxStr = (str1.length() > str2.length()) ? str1 : str2;
  108. String minStr = (str1.length() > str2.length()) ? str2 : str1;
  109. int len = minStr.length();
  110. for (int i = 0; i < len; i++) {// 0 1 2 3 4 此层循环决定要去几个字符
  111. for (int x = 0, y = len - i; y <= len; x++, y++) {
  112. if (maxStr.contains(minStr.substring(x, y))) {
  113. return minStr.substring(x, y);
  114. }
  115. }
  116. }
  117. }
  118. return null;
  119. }
  120. // 如果存在多个长度相同的最大相同子串
  121. // 此时先返回String[],后面可以用集合中的ArrayList替换,较方便
  122. public String[] getMaxSameSubString1(String str1, String str2) {
  123. if (str1 != null && str2 != null) {
  124. StringBuffer sBuffer = new StringBuffer();
  125. String maxString = (str1.length() > str2.length()) ? str1 : str2;
  126. String minString = (str1.length() > str2.length()) ? str2 : str1;
  127. int len = minString.length();
  128. for (int i = 0; i < len; i++) {
  129. for (int x = 0, y = len - i; y <= len; x++, y++) {
  130. String subString = minString.substring(x, y);
  131. if (maxString.contains(subString)) {
  132. sBuffer.append(subString + ",");
  133. }
  134. }
  135. System.out.println(sBuffer);
  136. if (sBuffer.length() != 0) {
  137. break;
  138. }
  139. }
  140. String[] split = sBuffer.toString().replaceAll(",$", "").split("\\,");
  141. return split;
  142. }
  143. return null;
  144. }
  145. // 如果存在多个长度相同的最大相同子串:使用ArrayList

// public List getMaxSameSubString1(String str1, String str2) { // if (str1 != null && str2 != null) { // List list = new ArrayList(); // String maxString = (str1.length() > str2.length()) ? str1 : str2; // String minString = (str1.length() > str2.length()) ? str2 : str1; // // int len = minString.length(); // for (int i = 0; i < len; i++) { // for (int x = 0, y = len - i; y <= len; x++, y++) { // String subString = minString.substring(x, y); // if (maxString.contains(subString)) { // list.add(subString); // } // } // if (list.size() != 0) { // break; // } // } // return list; // } // // return null; // }

  1. @Test
  2. public void testGetMaxSameSubString() {
  3. String str1 = "abcwerthelloyuiodef";
  4. String str2 = "cvhellobnmiodef";
  5. String[] strs = getMaxSameSubString1(str1, str2);
  6. System.out.println(Arrays.toString(strs));
  7. }
  8. // 第5题
  9. @Test
  10. public void testSort() {
  11. String str = "abcwerthelloyuiodef";
  12. char[] arr = str.toCharArray();
  13. Arrays.sort(arr);
  14. String newStr = new String(arr);
  15. System.out.println(newStr);
  16. }

}

  1. <a name="ltRH1"></a>
  2. # 3、**java.text.SimpleDateFormat类**
  3. Date类的API不易于国际化,大部分被废弃了,**java.text.SimpleDateFormat**类是一个不与语言环境有关的方式来格式化和解析日期的具体类。 <br />它允许进行**格式化:日期》文本**、**解析:文本》日期 **<br />**格式化: **<br />**SimpleDateFormat() **:默认的模式和语言环境创建对象 <br />**public SimpleDateFormat(String pattern):**该构造方法可以用参数pattern指定的格式创建一个对象,该对象调用: **public String format(Date date):**方法格式化时间对象date <br />**解析: **<br />**public Date parse(String source):**从给定字符串的开始解析文本,以生成一个日期。
  4. ---
  5. SimpleDateFormat的使用:SimpleDateFormat对日期Date类的格式化和解析<br />1、两个操作:<br />1.1 格式化:日期--->字符串<br />1.2 解析:格式化的逆过程,字符串--->日期<br />2、SimpleDateFormat的实例化
  6. ```java
  7. @Test
  8. public void test1() throws ParseException {
  9. SimpleDateFormat sdf = new SimpleDateFormat();
  10. //格式化
  11. Date date = new Date();
  12. System.out.println(date);
  13. String format = sdf.format(date);
  14. System.out.println(format);
  15. //解析
  16. String str = "2022-1-16 下午3:25";
  17. Date date1 = sdf.parse(str);
  18. System.out.println(date1);
  19. System.out.println("********************************");
  20. //指定方式格式化,调用带参的构造器
  21. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  22. //格式化
  23. String s = sdf1.format(date);
  24. System.out.println(s);
  25. //解析:要求字符串必须是符合SimpleDateFormat识别的格式(通过构造器参数体现),否则抛异常
  26. Date date2 = sdf1.parse("2019-11-28 16:44:25");
  27. System.out.println(date2);
  28. }
  1. /*
  2. 练习一:字符串“2020-09-08”转换为java.sql.Date
  3. */
  4. @Test
  5. public void test2() throws ParseException {
  6. String birth = "2020-09-08";
  7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  8. Date date = sdf.parse(birth);
  9. // System.out.println(date);
  10. java.sql.Date birthDate = new java.sql.Date(date.getTime());
  11. System.out.println(birthDate);
  12. }

4、java.util.Calendar(日历)类

Calendar是一个抽象基类,主用用于完成日期字段之间相互操作的功能。
获取Calendar实例的方法
使用Calendar.getInstance()方法
调用它的子类GregorianCalendar的构造器。
一个Calendar的实例是系统时间的抽象表示,通过get(int field)方法来取得想要的时间信息。比如YEAR、MONTH、DAY_OF_WEEK、HOUR_OF_DAY 、MINUTE、SECOND
public void set(int field,int value)
public void add(int field,int amount)
public final Date getTime()
public final void setTime(Date date)
注意:
获取月份时:一月是0,二月是1,以此类推,12月是11
获取星期时:周日是1,周二是2 , 。。。。周六是7


  1. @Test
  2. public void test1(){
  3. // Calendar的使用
  4. // 1、实例化
  5. // 方式一:创建其子类(GregorianCalendar)的对象
  6. // 方式二:调用其静态方法getInstance();
  7. Calendar calendar = Calendar.getInstance();
  8. System.out.println(calendar.getClass());//class java.util.GregorianCalendar
  9. Calendar calendar1 = new GregorianCalendar();
  10. // 2、常用方法
  11. // get()
  12. int days = calendar.get(Calendar.DAY_OF_MONTH);
  13. System.out.println(days);//16
  14. System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//16
  15. //
  16. // set()
  17. calendar.set(Calendar.DAY_OF_MONTH,5);
  18. days = calendar.get(Calendar.DAY_OF_MONTH);
  19. System.out.println(days);//5
  20. //
  21. // add()
  22. calendar.add(Calendar.DAY_OF_MONTH,-3);
  23. days = calendar.get(Calendar.DAY_OF_MONTH);
  24. System.out.println(days);//2
  25. //
  26. // getTime():日历类———>Date
  27. Date date = calendar.getTime();
  28. System.out.println(date.getTime());//1641111904994
  29. System.out.println(date);//Sun Jan 02 16:25:04 CST 2022
  30. //setTime():Date———>日历类
  31. Date date1 = new Date();
  32. calendar.setTime(date1);
  33. days = calendar.get(Calendar.DAY_OF_MONTH);
  34. System.out.println(days);//16
  35. }
  36. }

5、JDK8中新日期时间API

Java 8 吸收了 Joda-Time 的精华,以一个新的开始为 Java 创建优秀的 API。
新的 java.time 中包含了所有关于本地日期(LocalDate)、本地时间(LocalTime)、本地日期时间(LocalDateTime)、时区(ZonedDateTime) 和持续时间(Duration)的类。历史悠久的 Date 类新增了 toInstant() 方法, 用于把 Date 转换成新的表示形式。这些新增的本地化时间日期 API 大大简 化了日期时间和本地化的管理。

5.1 LocalDate、LocalTime、LocalDateTime

  1. package com.atguigu.java1;
  2. import org.junit.Test;
  3. import java.time.LocalDate;
  4. import java.time.LocalDateTime;
  5. import java.time.LocalTime;
  6. public class JDK8DateTimeTest {
  7. /*
  8. 说明:
  9. 1、LocalDateTime相较于LocalDate、LocalTime,使用频率要更高
  10. 2、类似于Calendar
  11. */
  12. @Test
  13. public void test1(){
  14. //now():获取当前的日期、时间、日期+时间
  15. LocalDate localDate = LocalDate.now();
  16. LocalTime localTime = LocalTime.now();
  17. LocalDateTime localDateTime = LocalDateTime.now();
  18. System.out.println(localDate);
  19. System.out.println(localTime);
  20. System.out.println(localDateTime);
  21. //of():设置指定的年月日,时分秒,没有偏移量。
  22. LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 16, 17, 31, 54);
  23. System.out.println(localDateTime1);
  24. //getXxx():获取相关的属性
  25. System.out.println(localDateTime.getDayOfWeek());
  26. System.out.println(localDateTime.getDayOfMonth());
  27. System.out.println(localDateTime.getMonth());
  28. System.out.println(localDateTime.getMonthValue());
  29. System.out.println(localDateTime.getMinute());
  30. //体现不可变性
  31. //withXxx():设置相关的属性
  32. LocalDate localDate1 = localDate.withDayOfMonth(22);
  33. System.out.println(localDate);
  34. System.out.println(localDate1);
  35. LocalDateTime localDateTime2 = localDateTime.withHour(4);
  36. System.out.println(localDateTime);
  37. System.out.println(localDateTime2);
  38. //plusXxx():
  39. LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
  40. System.out.println(localDateTime);
  41. System.out.println(localDateTime3);
  42. //minusXxx():
  43. LocalDateTime localDateTime4 = localDateTime.minusDays(6);
  44. System.out.println(localDateTime);
  45. System.out.println(localDateTime4);
  46. }
  47. }

QQ截图20220116175617.png

5.2 瞬时:Instant

Instant:时间线上的一个瞬时点。 这可能被用来记录应用程序中的事件时间戳。
QQ截图20220116180053.png
QQ截图20220116180126.png

  1. /*
  2. Instant的使用
  3. 类似于java.util.Date类
  4. */
  5. @Test
  6. public void test2(){
  7. //now():获取本初子午线对应的标准时间
  8. Instant instant = Instant.now();
  9. System.out.println(instant);
  10. //添加时间的偏移量
  11. OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
  12. System.out.println(offsetDateTime);
  13. //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数-->Date类的getTime()
  14. long milli = instant.toEpochMilli();
  15. System.out.println(milli);
  16. //ofEpochMilli():通过给定的毫秒数,获取Instant实例-->Date(long millis)
  17. Instant instant1 = Instant.ofEpochMilli(1642330764482L);
  18. System.out.println(instant1);
  19. }

5.3 格式化与解析日期或时间

QQ截图20220116190618.png

  1. /*
  2. DateTimeFormatter:格式化与解析日期或时间
  3. 类似于SimpleDateFormat
  4. */
  5. @Test
  6. public void test3(){
  7. //  预定义的标准格式。ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
  8. DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
  9. //格式化:日期-->字符串
  10. LocalDateTime localDateTime = LocalDateTime.now();
  11. String str1 = formatter.format(localDateTime);
  12. System.out.println(localDateTime);
  13. System.out.println(str1);
  14. //解析:字符串-->日期
  15. TemporalAccessor parse = formatter.parse("2022-01-16T19:15:43.29");
  16. System.out.println(parse);
  17. //  本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG)
  18. DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
  19. //格式化
  20. String str2 = formatter1.format(localDateTime);
  21. System.out.println(str2);
  22. DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
  23. //格式化
  24. String str3 = formatter2.format(LocalDate.now());
  25. System.out.println(str3);
  26. // 重点 自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
  27. DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  28. //格式化
  29. String str4 = formatter3.format(LocalDateTime.now());
  30. System.out.println(str4);
  31. //解析
  32. TemporalAccessor accessor = formatter3.parse("2022-01-16 19:32:46");
  33. System.out.println(accessor);
  34. }

5.4 其它API

QQ截图20220116193620.png
QQ截图20220116193830.png

6、 Java比较器

Java实现对象排序的方式有两种:
自然排序:java.lang.Comparable
定制排序:java.util.Comparator

  1. package com.atguigu.java2;
  2. public class Goods implements Comparable {
  3. private String name;
  4. private double price;
  5. public Goods() {
  6. }
  7. public Goods(String name, double price) {
  8. this.name = name;
  9. this.price = price;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public double getPrice() {
  18. return price;
  19. }
  20. public void setPrice(double price) {
  21. this.price = price;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Goods{" +
  26. "name='" + name + '\'' +
  27. ", price=" + price +
  28. '}';
  29. }
  30. @Override
  31. public int compareTo(Object o) {
  32. if (o instanceof Goods) {
  33. Goods goods = (Goods) o;
  34. //方式一
  35. if (this.price > goods.price) {
  36. return 1;
  37. } else if (this.price < goods.price) {
  38. return -1;
  39. } else {
  40. return 0;
  41. }
  42. //方式二
  43. // return Double.compare(this.price, goods.price);
  44. }
  45. throw new RuntimeException("传入的数据类型不一致!");
  46. }
  47. }
  1. package com.atguigu.java2;
  2. import org.junit.Test;
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. /**
  6. * 一、说明:java中的对象,正常情况下,只能进行比较:==或!=。不能使用</或>的
  7. * 但是在开发场景中,我们需要对多个对象进行排序,言外之意,就需要比较对象的大小。
  8. * 如何实现?使用两个接口中的任何一个:Comparable或Comparator
  9. * 二、Comparable接口与Comparator的使用对比:
  10. * Comparable接口的方式一旦指定,保证Comparable接口实现类的对象在任何位置都可以比较大小
  11. * Comparator接口属于临时性的比较。
  12. *
  13. *
  14. */
  15. public class ComparaTest {
  16. /*
  17. Comparable接口的使用举例:自然排序
  18. 1、像String、包装类等实现了Comparable接口,重写了compareTo()方法,给出了比较两个对象大小的方式
  19. 2、像String、包装类等重写compareTo(obj)方法以后,进行了从小到大的排序
  20. 3、重写compareTo(obj)方法的规则:
  21. 如果当前对象this大于形参对象obj,则返回正整数,
  22. 如果当前对象this小于形参对象obj,则返回负数
  23. 如果当前对象this等于形参对象obj,则返回零。
  24. 4、对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable接口,重写compareTo(obj)方法。
  25. 在compareTo(obj)方法中指明如何排序
  26. */
  27. @Test
  28. public void test1() {
  29. String[] arr = new String[]{"AA", "CC", "MM", "GG", "JJ", "DD", "KK"};
  30. Arrays.sort(arr);
  31. System.out.println(Arrays.toString(arr));
  32. }
  33. @Test
  34. public void test2() {
  35. Goods[] arr = new Goods[5];
  36. arr[0] = new Goods("lianxiangMouse", 34);
  37. arr[1] = new Goods("dellMouse", 43);
  38. arr[2] = new Goods("xiaomiMouse", 12);
  39. arr[3] = new Goods("huaweiMouse", 65);
  40. arr[4] = new Goods("microsoftMouse", 43);
  41. Arrays.sort(arr);
  42. System.out.println(Arrays.toString(arr));
  43. }
  44. /*
  45. Comparator接口的使用举例:定制排序
  46. 1、背景:
  47. 当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,
  48. 或者实现了java.lang.Comparable接口的排序规则不适合当前的操作,
  49. 那么可以考虑使用 Comparator 的对象来排序。
  50. 2、重写compare(Object o1,Object o2)方法,比较o1和o2的大小:
  51. 如果方法返回正整数,则表示o1大于o2;
  52. 如果返回0,表示相等;
  53. 返回负整数,表示o1小于o2。
  54. */
  55. @Test
  56. public void test3() {
  57. String[] arr = new String[]{"AA", "CC", "MM", "GG", "JJ", "DD", "KK"};
  58. Arrays.sort(arr, new Comparator<String>() {
  59. //按照字符串从大到小的顺序排列
  60. @Override
  61. public int compare(String o1, String o2) {
  62. if (o1 instanceof String && o2 instanceof String) {
  63. String s1 = (String) o1;
  64. String s2 = (String) o2;
  65. return -s1.compareTo(s2);
  66. }
  67. throw new RuntimeException("输入的数据类型不一致");
  68. }
  69. });
  70. }
  71. @Test
  72. public void test4(){
  73. Goods[] arr = new Goods[6];
  74. arr[0] = new Goods("lianxiangMouse", 34);
  75. arr[1] = new Goods("dellMouse", 43);
  76. arr[2] = new Goods("xiaomiMouse", 12);
  77. arr[3] = new Goods("huaweiMouse", 65);
  78. arr[4] = new Goods("huaweiMouse", 224);
  79. arr[5] = new Goods("microsoftMouse", 43);
  80. Arrays.sort(arr, new Comparator<Goods>() {
  81. //指明商品比较大小的方式:按照产品名称从低到高排序,再按照价格从高到低排序
  82. @Override
  83. public int compare(Goods o1, Goods o2) {
  84. if (o1 instanceof Goods && o2 instanceof Goods) {
  85. Goods g1 = (Goods) o1;
  86. Goods g2 = (Goods) o2;
  87. if (g1.getName().equals(g2.getName())){
  88. return -Double.compare(g1.getPrice(), g2.getPrice());
  89. }else{
  90. return g1.getName().compareTo(g2.getName());
  91. }
  92. }
  93. throw new RuntimeException("输入的数据类型不一致");
  94. }
  95. });
  96. System.out.println(Arrays.toString(arr));
  97. }
  98. }

7、System类

QQ截图20220116210058.png
QQ截图20220116210118.png

  1. package com.atguigu.java2;
  2. import org.junit.Test;
  3. /*
  4. 其他常用类的使用
  5. 1、System
  6. 2、Math
  7. 3、BigInteger 和BigDecimal
  8. */
  9. public class OtherClassTest {
  10. @Test
  11. public void test1() {
  12. String javaVersion = System.getProperty("java.version");
  13. System.out.println("java的version:" + javaVersion);
  14. String javaHome = System.getProperty("java.home");
  15. System.out.println("java的home:" + javaHome);
  16. String osName = System.getProperty("os.name");
  17. System.out.println("os的name:" + osName);
  18. String osVersion = System.getProperty("os.version");
  19. System.out.println("os的version:" + osVersion);
  20. String userName = System.getProperty("user.name");
  21. System.out.println("user的name:" + userName);
  22. String userHome = System.getProperty("user.home");
  23. System.out.println("user的home:" + userHome);
  24. String userDir = System.getProperty("user.dir");
  25. System.out.println("user的dir:" + userDir);
  26. }
  27. }

8、Math类

QQ截图20220116210604.png

9、BigInteger类和BigDecimal类

QQ截图20220116210723.png
QQ截图20220116210848.png


QQ截图20220116210935.png

  1. @Test
  2. public void test2() {
  3. BigInteger bi = new BigInteger("12433241123");
  4. BigDecimal bd = new BigDecimal("12435.351");
  5. BigDecimal bd2 = new BigDecimal("11");
  6. System.out.println(bi);
  7. // System.out.println(bd.divide(bd2));
  8. System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP));
  9. System.out.println(bd.divide(bd2, 15, BigDecimal.ROUND_HALF_UP));
  10. }
  11. }