跳转至

常用类

一、字符串相关

1.String

1.1 String的不可变性

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * 1. 声明为final的,不可被基础
 * 2. 实现了Serializable接口:表示字符串是支持序列化的
 * 3. 实现了Comparable接口:表示字符串可以比较大小
 * 4. 内部定义了final char[] value用于存储字符串数据
 * 5. Sting:代表不可变的字符序列,简称:不可变性
 *    当对字符串重新赋值时,需要重新指定内存区域赋值,不能在原有的value进行赋值
 *    当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能在原有的value进行赋值
 *    当调用String的replace方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能在原有的value进行赋值
 * 6. 通过字面量的方法(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
 * 7. 字符串常量池中的不会存储相同内容的字符串的
 */
public class StringTest {
    public static void main(String[] args) {
        String str1 = "abc"; // 字面量的定义方式
        String str2 = "abc";
        System.out.println(str1 == str2); // true 地址值相同
        str1 = "hello";

        System.out.println(str1); // hello
        System.out.println(str2); // abc

        System.out.println("*************");

        String str3 = "abc";
        str3 += "def";
        System.out.println(str3); // abcdef
        System.out.println(str2); // abc

        System.out.println("*************");
        String str4 = "abc";
        String str5 = str4.replace("a","A");
        System.out.println(str4);  // abc
        System.out.println(str5);  // Abc
    }
}

1.2 不同实例化方法的对比

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * String的实例化方法
 * 1. 通过字面量定义的方法
 * 2. 通过new 构造器的方法
 *
 */
public class StringTest {
    public static void main(String[] args) {
        // str1 和 str2 的数据声明在方法区的字符串常量池中
        String str1 = "abc";
        String str2 = "abc";
        // str3 和 str4 保存的地址值,是数据在堆空间开辟空间以对应的地址值
        String str3 = new String("abc");
        String str4 = new String("abc");

        System.out.println(str1 == str2); // true
        System.out.println(str3 == str4); // false

        Person p1 = new Person("Tom");
        Person p2 = new Person("Tom");

        System.out.println(p1.name == p2.name); // true
    }
}


class Person {
    String name;

    public Person(String name) {
        this.name = name;
    }
}

1.3 不同拼接方式的对比

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * 常量与常量的拼接结果在常量池,且常量池中不会存在相同内容的变量
 * 只有其中有一个变量,结果就在堆中
 * 如果拼接的结果调用intern()方法,返回值就在常量池中
 */
public class StringTest {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "world";

        String str3 = "helloworld";
        String str4 = "hello" + "world"; // 字面量的连接
        String str5 = str1 + "world";
        String str6 = "hello" + str2;
        String str7 = str1 + str2;

        System.out.println(str3 == str4); // true
        System.out.println(str3 == str5); // false
        System.out.println(str3 == str6); // false
        System.out.println(str3 == str7); // false
        System.out.println(str5 == str6); // false
        System.out.println(str5 == str7); // false
        System.out.println(str6 == str7); // false

        String str8 = str5.intern(); // 返回值得到的str8使用的是常量池中已存在的helloworld
        System.out.println(str3 == str8); // true
    }
}

1.4 String常用方法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Locale;

public class StringMethodTest {
    public static void main(String[] args) {
        String str1 = "Hello World";
        String str2 = "hello world";
        // 返回字符串长度
        System.out.println(str1.length()); // 11
        // 返回某索引位置的字符
        System.out.println(str1.charAt(1)); // e
        // 判断字符串是否是空字符串
        System.out.println(str1.isEmpty()); // false
        // 使用默认语言环境,将所有字符串转为小写
        System.out.println(str1.toLowerCase()); // hello world
        // 使用默认语言环境,将所有字符串转为大写
        System.out.println(str1.toUpperCase()); // HELLO WORLD
        // 去除字符串前后空白
        System.out.println(str1.trim()); // Hello World
        // 字符串拼接,相当于用“+”
        System.out.println(str1.concat("!")); // Hello World!
        // 比较两个字符串的大小
        System.out.println(str1.compareTo(str2)); // -32
        // 返回现有字符串的子串
        System.out.println(str1.substring(6)); // World
        System.out.println(str1.substring(6, 8)); // Wo  左闭右开
        // 判断是否以指定后缀结束
        System.out.println(str1.endsWith("W")); // false
        // 判断是否以指定后缀开始
        System.out.println(str1.startsWith("He")); // true
        // 判断从指定序索引位置开始,是否以指定后缀开始
        System.out.println(str1.startsWith("W", 6)); // true
        // 判断当前字符串是否包含指定字符串
        System.out.println(str1.contains("ll")); // true
        // 返回指定字符串在此字符串中第一次出现处的索引
        System.out.println(str1.indexOf("l")); // 2  找不到返回-1
        // 从指定索引位置开始查找,返回指定字符串在此字符串中第一次出现处的索引
        System.out.println(str1.indexOf("ld", 5)); // 9
        // 返回指定字符串在此字符串中第一次出现处的索引(从右往左)
        System.out.println(str1.lastIndexOf("l")); // 9
        // 从指定索引位置反向开始查找,返回指定字符串在此字符串中第一次出现处的索引(从右往左)
        System.out.println(str1.lastIndexOf("lo", 7)); // 3

        //替换
        // 替换字符
        System.out.println(str1.replace('o', 'e')); // Helle Werld
        // 替换字符串
        System.out.println(str1.replace("ll", "LL")); // HeLLo World
        // 正则匹配替换
        System.out.println(str1.replaceAll("l[a-z]", "xx")); // Hexxo Worxx
        // 判断字符串是否匹配正则表达式
        System.out.println(str1.matches("\\S{5}\\s\\S{5}")); // true
        //切片
        String[] strs = str1.split(" "); // 以空格分隔
        for (int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
        // Hello
        // World
    }
}

1.5 Stringchar[]之间的转换

String转换为char[]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public class StringToCharTest {
    public static void main(String[] args) {
        // String 与 Char[] 之间的转换
        String str = "Hello World";
        char[] charArr = str.toCharArray();
        for (int i = 0; i < charArr.length; i++) {
            System.out.println(charArr[i]);
        }
    }
}

char[]转换为String

1
2
3
4
5
6
7
public class StringToCharTest {
    public static void main(String[] args) {
        char[] charArr = new char[]{'h', 'e', 'l', 'l', 'o'};
        String str = new String(charArr);
        System.out.println(str); // hello
    }
}

1.6 Stringbyte[]之间的转换

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.util.Arrays;

public class StringToCharTest {
    public static void main(String[] args) {
        String str = "abc123";
        byte[] strBytes = str.getBytes();  // 使用默认的字符集进行编码
        System.out.println(Arrays.toString(strBytes)); // [97, 98, 99, 49, 50, 51]
        String str1 = new String(strBytes);
        System.out.println(str1); // abc123  // 解码

    }
}
// 编码和解码时使用的字符集要一致

2.StringBufferStringBuilder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// StringBuffer: 线程安全的,效率低
// StringBuilder: 线程不安全的,效率高
// 底层都使用char[]存储
// 可都是变的字符序列

public class StringBufferTest {
    public static void main(String[] args) {
        // new StringBuffer()  // new char[16] 底层创建了一个长度是16的数组
        StringBuffer str = new StringBuffer("hollo"); // new char["abc".length() + 16]
        str.setCharAt(1, 'e');
        System.out.println(str); // hello   // 可变的字符序列
        System.out.println(str.length());  // 5
        // 如果要添加的数据底层数据盛不下,就需要扩容底层的数组。
        // 默认情况下,扩容为原来容量的2倍+2,同时将原有的数组中的元素赋值到新的数组中。

        // 在开发中建议使用 StringBuffer(int capacity)

        // 常用方法

        str.append(" world");
        System.out.println(str); // hello world
        str.delete(10, 11);
        System.out.println(str); // hello worl
        str.replace(6, 10, "xx");
        System.out.println(str); // hello xx
        str.insert(8, '!');
        System.out.println(str); // hello xx!
        str.reverse();
        System.out.println(str); // !xx olleh
    }
}

三者的效率:StringBuilder>StringBuffer>String

二、日期和时间

1.System.currentTimeMillis

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// JDK8 之前
import java.util.Date;

import static java.lang.System.currentTimeMillis;

public class DateTimeTest {
    public static void main(String[] args) {

        long timestamp =  currentTimeMillis();
        System.out.println(timestamp); // 1636377265852
    }
}

2.java.util.Date

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// JDK8 之前
import java.util.Date;

public class DateTimeTest {
    public static void main(String[] args) {

        /*
        * java.util.Date类
        * 1. 两个构造器的使用
        * 2. 两个方法的使用:toString();  getTime()
        */
        // 创建一个对应当前时间的Date对象
        Date date1 = new Date();
        System.out.println(date1.toString());  // Mon Nov 08 21:14:25 CST 2021
        System.out.println(date1.getTime());  // 1636377265853
        // 创建指定毫秒数的Date对象
        Date date2 = new Date(1636377265853L);
        System.out.println(date2);

        // java.sql.Date() 对应数据库中日期类型的变量
        java.sql.Date date3 = new java.sql.Date(1636377265853L);
        System.out.println(date3.toString());  // 2021-11-08

        // java.util.Date对象转换为java.sql.Date对象
        Date date4 = new Date();
        java.sql.Date date5 = new java.sql.Date(date4.getTime());
        System.out.println(date5);  // 2021-11-08
    }
}

3.SimpleDateFormat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// JDK8以前
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        // JDK8 之前
        /*
         * 格式化: 日期-->字符串
         * 解析:字符串-->日期
         */
        SimpleDateFormat sdf = new SimpleDateFormat();

        Date date = new Date();
        System.out.println(date);  // Mon Nov 08 21:43:34 CST 2021
        String format = sdf.format(date);
        System.out.println(format); // 21-11-8 下午9:43

        // 解析
        String str = "21-11-8 下午9:47";
        Date date1 = sdf.parse(str);
        System.out.println(date1); // Mon Nov 08 21:47:00 CST 2021


        // 带参数的构造器
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1 = sdf1.format(date);
        System.out.println(format1);  // 2021-11-08 09:55:19

        // 解析
        Date date2 = sdf1.parse("2021-11-08 09:55:19");
        System.out.println(date2); // Mon Nov 08 09:55:19 CST 2021
    }
}

4.Calendar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// JDK8以前
import java.util.Calendar;
import java.util.Date;

public class CalendarTest {
    public static void main(String[] args) {
        // Calendar是一个抽象类
        // 方法一:创建其子类的对象
        // 方法二:调用静态方法 getInstance()
        Calendar calendar = Calendar.getInstance();

        // 常用方法
        // get()
        int month = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(month); // 8
        // set()  // Calendar是可变的
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); // 1
        // add()
        calendar.add(Calendar.DAY_OF_MONTH, 2); // 3
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        //getTime()
        Date date = calendar.getTime();
        System.out.println(date);  // Wed Nov 03 22:15:35 CST 2021
        //setTime()
        Date now = new Date();
        calendar.setTime(now);
        System.out.println(calendar.getTime());  // Mon Nov 08 22:16:55 CST 2021
    }
}

5. LocalDateLocalTimeLocalDateTime

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// JDK8 特性
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class JDK8DateTimeTest {
    public static void main(String[] args) {
        // now()获取当前的日期和时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now(); // 更常用

        System.out.println(localDate); // 2021-11-16
        System.out.println(localTime); // 22:07:40.540
        System.out.println(localDateTime); // 2021-11-16T22:07:40.540

        // of 设置指定的年月日时分秒,没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2021, 10, 1, 10, 00, 10);
        System.out.println(localDateTime1);  // 2021-10-01T10:00:10


        // getxx()
        System.out.println(localDateTime.getDayOfYear()); // 320
        System.out.println(localDateTime.getDayOfMonth());  // 16
        System.out.println(localDateTime.getDayOfWeek());  // TUESDAY
        System.out.println(localDateTime.getMonth());  // NOVEMBER

        // withxx 设置时间相关的属性
        LocalDateTime localDateTime2 = localDateTime1.withDayOfMonth(12);
        System.out.println(localDateTime1); // 2021-10-01T10:00:10
        System.out.println(localDateTime2); // 2021-10-12T10:00:10

        // 时间加减
        System.out.println(localDateTime.plusHours(1)); // 2021-11-16T23:18:42.867  加时间
        System.out.println(localDateTime.minusHours(1)); // 2021-11-16T21:19:44.087  减时间
    }
}

6.Instant

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class InstantTest {
    public static void main(String[] args) {
        Instant instant = Instant.now();
        System.out.println(instant);  // 2021-11-16T14:24:13.265Z    本初子午线时间

        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);  // 2021-11-16T22:27:34.906+08:00 东八区时间

        // 自1970年1月1日0时0分0秒开始的毫秒数
        System.out.println(instant.toEpochMilli());  // 1637072931402

        // 通过给定的毫秒数获取Instant实例
        Instant instant1 = Instant.ofEpochMilli(1637072931402L);
        System.out.println(instant1);  // 2021-11-16T14:28:51.402Z
    }
}

7. DateTimeFormatter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;

public class DateTimeFormatterTest {
    public static void main(String[] args) {
        // 实例化方式一:预定义的标准格式  较少使用
        DateTimeFormatter formatter1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        // 格式化:日期-->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String formatStr1 = formatter1.format(localDateTime);
        System.out.println(formatStr1);  // 2021-11-16T22:37:41.47
        // 解析:字符串-->日期
        TemporalAccessor parse1 = formatter1.parse("2021-11-16T22:37:41.47");
        System.out.println(parse1);  // {},ISO resolved to 2021-11-16T22:37:41.470

        // 实例化方式二:本地化相关的格式  较少使用
        // FormatStyle.LONG: 2021年11月16日 下午10时44分05秒
        // FormatStyle.MEDIUM: 2021-11-16 22:44:59
        // FormatStyle.SHORT: 21-11-16 下午10:45
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
        String formatStr2 = formatter2.format(localDateTime);
        System.out.println(formatStr2);

        // 实例化方式三:自定义的格式
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        String formatStr3 = formatter3.format(LocalDateTime.now());
        System.out.println(formatStr3); // 2021-11-16 10:50:36
        // 解析
        TemporalAccessor parse2 = formatter3.parse("2021-11-16 10:50:36");
        System.out.println(parse2);  // {MinuteOfHour=50, HourOfAmPm=10, MilliOfSecond=0, MicroOfSecond=0, SecondOfMinute=36, NanoOfSecond=0},ISO resolved to 2021-11-16
    }
}

三、Java比较器

1.Comparable自然排序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Java 中的对象,正常情况下只能进行==、!= 比较
 * 不能使用 > 或 <
 * 但是在开发场景中,需要对多个对象进行排序,就需要比较对象的大小。
 * Java中可以使用Comparable或Comparator接口
 */

import java.util.Arrays;

/*
 Comparable 接口的使用 (自然排序)

 像String、包装类等实现了Comparable接口,重写了CompareTo()方法,给出了比较两个对象大小的方式
 进行从小到大的排序

 重写CompareTo()的规则:
    1. 如果当前对象this大于形参对象obj,则返回正整数;
        如果当前对象this小于形参对象obj,则返回负整数;
        如果当前对象this等于形参对象obj,则返回零。

 对于自定义类来说,如果需要排序,需要自定义类实现Comparable接口,重写CompareTo()方法
 在CompareTo(obj)中指明如何排序。
*/

public class ComparableTest {
    public static void main(String[] args) {
        String[] arr1 = new String[]{"AA", "ZZ", "CC", "XX"};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));  // [AA, CC, XX, ZZ]


        Goods[] arr2 = new Goods[5];
        arr2[0] = new Goods("OPPO", 3199);
        arr2[1] = new Goods("Iphone", 6999);
        arr2[2] = new Goods("Vivo", 3199);
        arr2[3] = new Goods("Xiaomi", 1999);
        arr2[4] = new Goods("Huawei", 5999);

        Arrays.sort(arr2);
        System.out.println(Arrays.toString(arr2));
        // [Goods{name='Xiaomi', price=1999.0}, Goods{name='OPPO', price=3199.0}, Goods{name='Vivo', price=3199.0}, Goods{name='Huawei', price=5999.0}, Goods{name='Iphone', price=6999.0}]
    }
}

class Goods implements Comparable {
    private String name;
    private double price;

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }


    // 指明商品比较大小的方式:按照价格从低到高排序
    // 再按照商品名称从低到高排序
    @Override
    public int compareTo(Object o) {
        if (o instanceof Goods) {
            Goods goods = (Goods) o;
            if (this.price > goods.price) {
                return 1;
            } else if (this.price < goods.price) {
                return -1;
            } else {
                return this.name.compareTo(goods.name);
                // return 0;
            }
        }
        throw new RuntimeException("传入的数据类型不一致!");
    }
}

2.Comparator定制排序

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.Arrays;
import java.util.Comparator;

//Comparator 定制排序
/*
 * 1. 背景:
 *   当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,
 *   或者实现了java.lang.Comparable接口的排序规则不适合当前的操作,
 *   那么可以考虑使用Comparator的对象来排序。
 * 2. 重写compare(Object o1, Object o2)方法,比较o1和o2的大小
 *    如果方法返回正整数,则表示o1大于o2;
 *    如果返回0,表示相等;
 *    如果返回负整数,表示o1小于o2。
 *
 */
public class ComparatorTest {
    public static void main(String[] args) {
        String[] arr1 = new String[]{"AA", "ZZ", "CC", "XX"};
        Arrays.sort(arr1, new Comparator<String>() {
            // 按照字符串从大到小的顺序排列
            @Override
            public int compare(String o1, String o2) {
                if (o1 instanceof String && o2 instanceof String) {
                    String s1 = (String) o1;
                    String s2 = (String) o2;
                    return -s1.compareTo(s2);
                }
                throw new RuntimeException("输入的数据类型不一致");
            }
        });
        System.out.println(Arrays.toString(arr1));  // [ZZ, XX, CC, AA]

        Goods[] arr2 = new Goods[5];
        arr2[0] = new Goods("OPPO", 3199);
        arr2[1] = new Goods("Iphone", 6999);
        arr2[2] = new Goods("Iphone", 9999);
        arr2[3] = new Goods("Xiaomi", 1999);
        arr2[4] = new Goods("Huawei", 5999);

        Arrays.sort(arr2, new Comparator() {
            // 指明商品比较大小的方式:先按照商品名称从低到高,在按照价格从高到低排序
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Goods && o2 instanceof Goods) {
                    Goods g1 = (Goods) o1;
                    Goods g2 = (Goods) o2;
                    if (g1.getName().equals(g2.getName())) {
                        return -Double.compare(g1.getPrice(), g2.getPrice());
                    } else {
                        return g1.getName().compareTo(g2.getName());
                    }
                }
                throw new RuntimeException("输入的数据类型不一致");
            }
        });
        System.out.println(Arrays.toString(arr2)); // [Goods{name='Huawei', price=5999.0}, Goods{name='Iphone', price=9999.0}, Goods{name='Iphone', price=6999.0}, Goods{name='OPPO', price=3199.0}, Goods{name='Xiaomi', price=1999.0}]
    }
}


class Goods {
    private String name;
    private double price;

    @Override
    public String toString() {
        return "Goods{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }
}

四、System

1
2
3
4
5
public class SystemTest {
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.version")); // 1.8.0_291
    }
}

五、Math

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class MathTest {
    public static void main(String[] args) {
        System.out.println("90 度的正弦值:" + Math.sin(Math.PI/2));
        System.out.println("0度的余弦值:" + Math.cos(0));
        System.out.println("60度的正切值:" + Math.tan(Math.PI/3));
        System.out.println("1的反正切值: " + Math.atan(1));
        System.out.println("π/2的角度值:" + Math.toDegrees(Math.PI/2));
        System.out.println(Math.PI);
//        90 度的正弦值:1.0
//        0度的余弦值:1.0
//        60度的正切值:1.7320508075688767
//        1的反正切值: 0.7853981633974483
//        π/2的角度值:90.0
//        3.141592653589793
    }
}

六、BigIntegerBigDecimal