字符串-Date-UUID-BigDecimal

  1. 1. String 补充
    1. 1.1. 转换示意图
    2. 1.2. 案例
  2. 2. StringBuilder
    1. 2.1. 含义
    2. 2.2. 使用场景
    3. 2.3. 常用方法
    4. 2.4. 案例
  3. 3. StringBuffer
    1. 3.1. 含义
    2. 3.2. 特点
    3. 3.3. 案例
  4. 4. Date
    1. 4.1. 含义
    2. 4.2. 常用方法
    3. 4.3. 案例
  5. 5. Calendar
    1. 5.1. 含义
    2. 5.2. 常用方法
    3. 5.3. 案例
  6. 6. SimpleDateFormat
    1. 6.1. 含义
    2. 6.2. 常用方法
    3. 6.3. 常见的格式
    4. 6.4. 案例
  7. 7. UUID
    1. 7.1. 含义
    2. 7.2. 使用场景
    3. 7.3. 案例
  8. 8. 享元设计模式
    1. 8.1. 含义
    2. 8.2. 目的
    3. 8.3. 步骤
    4. 8.4. 案例
  9. 9. 包装数据类型
    1. 9.1. 含义
    2. 9.2. 为什么需要有
    3. 9.3. 对应关系
    4. 9.4. 装箱
    5. 9.5. 拆箱
    6. 9.6. 常用方法
    7. 9.7. 案例一
    8. 9.8. 案例二
    9. 9.9. 案例三
  10. 10. Sysetem[了解]
    1. 10.1. 含义
    2. 10.2. 案例
  11. 11. Runtime[了解]
    1. 11.1. 含义
    2. 11.2. 案例
  12. 12. BigDecimal
    1. 12.1. 含义
    2. 12.2. 注意点
    3. 12.3. 常用方法
    4. 12.4. 案例
  13. 13. BigInteger
    1. 13.1. 含义
    2. 13.2. 案例
  14. 14. DecimalFormat
    1. 14.1. 含义
    2. 14.2. 案例
  15. 15. 集合
    1. 15.1. 含义
    2. 15.2. 数组特点
    3. 15.3. 集合特点
    4. 15.4. 示意图
  16. 16. ArrayList
    1. 16.1. 含义
    2. 16.2. 底层
    3. 16.3. 体系结构
    4. 16.4. 常用方法
    5. 16.5. 案例
  17. 17. String的equals 跟equalsIgnoreCase区别?

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
public class StringDemo {


public static void main(String[] args) {

String name = "abc";

char[] values = name.toCharArray();

System.out.println(Arrays.toString(values));


String otherName = new String(values);
System.out.println(otherName);

otherName = new String(values, 1, 2);
System.out.println(otherName);


System.out.println(name);
byte [] bytes = name.getBytes();

System.out.println(Arrays.toString(bytes));

otherName = new String(bytes, 0, 2);
System.out.println(otherName);
}

}

StringBuilder

含义

可变字符串

使用场景

做字符串拼接等操作

常用方法

  • new StringBuilder()
  • new StringBuilder(CharSequence data)
  • append
  • insert
  • deleteCharAt
  • reverse
  • 等等大量和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
public class StringBuilderDemo {

//String
//含义:不可变字符串

//StringBuilder
//含义:可变字符串
//作用:字符串的拼接
//常用的方法和String差不多

public static void main(String[] args) {
String name = "11";
System.out.println(name.hashCode());

name = name + "22";
System.out.println(name.hashCode());

StringBuilder SB = new StringBuilder();
/*
SB.append("11");
SB.append("22");
*/

//验证拼接内容之后有没有产生新的对象
System.out.println(SB.hashCode());
SB.append("11").append("22").append("33");
System.out.println(SB.toString());
System.out.println(SB.hashCode());

//删除指定字符
SB.deleteCharAt(0);

System.out.println(SB);

//插入指定字符
SB.insert(0,"1");
System.out.println(SB);

//字符串逆序
SB.reverse();
System.out.println(SB);

//需求将如下数组逆序摆放
byte [] xx = {97,98,99};
StringBuilder data = new StringBuilder(new String(xx));
data.reverse();
xx = data.toString().getBytes();
System.out.println(Arrays.toString(xx));
}
}

StringBuffer

含义

线程安全的可变字符串

特点

  • 方法几乎都加了synchronized,代表多线程环境下是线程安全的
  • 其他方法几乎和StringBuilder一样

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
public class StringBufferDemo {
//StringBuffer
//含义:和StringBuilder一样都是用来做字符串的拼接
//区别:线程安全

public static void main(String[] args) {

StringBuffer SB = new StringBuffer("abc");
SB.append("efg");

System.out.println(SB);
}
}

Date

含义

日期类

常用方法

  • new Date()
  • after
  • before
  • compareTo
  • getTime
  • setTime

案例

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
public class DateDemo {
//Date
//含义:时间、日期类
//常用方法
//

public static void main(String[] args) {

//Comparable

//代表当前时间
Date dateOne = new Date();
//常用的方法
System.out.println(dateOne.getTime());

Date dateTwo = new Date(dateOne.getTime()-1000000);

System.out.println(dateOne.after(dateTwo));

System.out.println(dateOne.before(dateTwo));

System.out.println(dateOne.compareTo(dateTwo));

System.out.println(dateOne);

yy();
}

//代表该方法过时,不建议使用
@Deprecated
private static void yy(){

}
}

Calendar

含义

日期的操作类

常用方法

  • Calendar.getInstance()
  • getTime
  • setTime
  • before
  • after
  • compareTo
  • add(int field,int value)
  • get(int field)

案例

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
package com.neu.day07._04date;

import java.util.Calendar;
import java.util.Date;

public class CalendarDemo {


//Calendar
//含义:时间的操作类

public static void main(String[] args) {

//当前时间的calendar
Calendar calendar = Calendar.getInstance();

Date curDate = calendar.getTime();

//calendar.setTime(date);


System.out.println(calendar.get(Calendar.YEAR));
System.out.println(calendar.get(Calendar.MONTH)+1);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));


System.out.println(calendar.get(Calendar.DAY_OF_YEAR));


int week = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(week);


//TODO 每个同学自行补全,显示今天星期几
switch (week) {
case Calendar.SATURDAY:

break;
case Calendar.THURSDAY:

break;

default:
break;
}

calendar.add(Calendar.HOUR,-1);
System.out.println(calendar.getTime());

//calendar.before(when)
//calendar.after(when)
//calendar.compareTo(anotherCalendar)

}
}

SimpleDateFormat

含义

日期的格式化类

常用方法

  • applyPattern
  • format
  • parse

常见的格式

案例

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
public class SimpleDateFormateDemo {


//含义
//日期的格式化类
//常用方法
//formate
//parse
//注意点
//解析的时候,解析格式要和被解析的时间字符串要兼容

public static void main(String[] args) {

Date date= new Date();

System.out.println(date);

//

/*
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
*/

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = simpleDateFormat.format(date);

System.out.println(result);


//String-->Date对象

//Parse 解析

//此处为什么会报错?该如何修复呢?
SimpleDateFormat otherFormat = new SimpleDateFormat("yyyy年MM月dd日");

try {
Date otherDate = otherFormat.parse(result);
System.out.println(simpleDateFormat.format(otherDate));
} catch (ParseException e) {
e.printStackTrace();
}
}
}

UUID

含义

随机数

使用场景

数据库主键(char(32))

案例

1
2
3
4
5
6
7
8
9
10
public class UUIDDemo {

public static void main(String[] args) {
String ID = UUID.randomUUID().toString();
System.out.println(ID.length());
System.out.println(ID);
System.out.println(ID.replace("-",""));
System.out.println(ID.replace("-","").length());
}
}

享元设计模式

含义

将那些经常使用的对象缓存起来,使用的时候直接从缓存中获取

目的

避免反复创建那些经常使用的对象

步骤

  • 定义一个缓存容器[比如:数组]
  • 静态代码块中创建那些经常使用的对象并放入缓存容器中
  • 对外提供从容器中获取对象的方法

案例

User.java

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
public class User {

//这几个用户经常使用
//先提前创建好并放入缓存,使用的时候直接从缓存中获取?
private static User [] cache = new User[3];

static{
User zs = new User("zs");
User ls = new User("ls");
User ww = new User("ww");

cache[0]=zs;
cache[1]=ls;
cache[2]=ww;
}

public static User valueOf(String name){
User result = null;
for(User user : cache){
if(user.name.equals(name)){
result = user;
break;
}
}

if(result == null){
result = new User(name);
}

return result;
}

private String name;

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

@Override
public String toString() {
return "User [name=" + name + "]";
}
}

Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Test {

public static void main(String[] args) {

//享元设计模式
//含义:把经常使用的对象提前创建好,使用的时候直接从缓存中获取并返回
//好处:避免反复创建那些经常使用的对象。

User userOne = User.valueOf("zs");
User userTwo = User.valueOf("zs");

System.out.println(userOne == userTwo);

User userThree = User.valueOf("xx");
User userFour = User.valueOf("xx");

System.out.println(userThree == userFour);
}
}

包装数据类型

含义

8大基本数据类型都有对应的引用数据类型,这些引用数据类型一般称为包装数据类型

为什么需要有

  • 包装数据类型可以表示某些特定的状态
  • 包装数据类型提供了一些好用的方法

对应关系

基本数据类型 包装数据类型
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean

装箱

自行不全

拆箱

自行补全

常用方法

  • intValue
  • valueOf
  • parseInt
  • toBinaryString
  • toHexString
  • max
  • min
  • compareTo
  • MAX_VALUE
  • MIN_VALUE
  • SIZE

案例一

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
package com.neu.day07._07wrapper;

public class IntegerDemo {

//包装数据类型
//含义:8大基本数据类型都有对应的引用数据类型
//对应关系
//byte Byte
//short Short
//char Character
//int Integer
//float Float
//dobule Double
//boolean Boolean

//为什么要有?
//包装数据类型提供了很多好用方法和属性
//intValue
//valueOf
//parseInt

//toBinaryString
//toHexString
//max
//min
//compareTo
//MAX_VALUE
//MIN_VALUE
//SIZE


//包装数据可以表示一些特殊的含义
//比如表示用户没有参加考试

//基本数据《---》包装数据类型之间的转换??
//手动
//装箱:基本数据--->包装数据类型
//拆箱:包装数据类型--->基本数据
//自动
//装箱:基本数据--->包装数据类型
//拆箱:包装数据类型--->基本数据

public static void main(String[] args) {

//手动装箱
Integer data = Integer.valueOf(10);

//手动拆箱
int dataOther = data.intValue();

//自动装箱 [经过反编译发现本质还是调用valueOf方法]
Integer num = 20;

//自动拆箱 [经过反编译发现本质还是调用intValue方法]
int numOther = num;


//好用的方法
System.out.println(Integer.parseInt("111"));

System.out.println(Integer.toBinaryString(140));

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

System.out.println(Integer.SIZE/8);

Integer one = 110;
Integer two = 120;

System.out.println(one > two);

System.out.println(one.compareTo(two));

System.out.println(Integer.toHexString(12));


System.out.println(Integer.min(one, two));
System.out.println(Integer.max(one, two));


Integer temp = new Integer(20);
}
}

案例二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class IntegerDemoOther {
public static void main(String[] args) {

Integer data1 = 1; //自动装箱[Integer.valueOf(int)]
Integer data2 = 1;
System.out.println(data1==data2); //true


Integer data3 = 127; //自动装箱[Integer.valueOf(int)]
Integer data4 = 127;
System.out.println(data3==data4); //true


Integer data5 = 128; //自动装箱[Integer.valueOf(int)]
Integer data6 = 128;
System.out.println(data5==data6); //false

Integer data7 = new Integer(1);
Integer data8 = new Integer(1);

System.out.println(data7==data8); //false
}
}

案例三

1
2
3
总结发现byte\short\int\long对应的包装数据类型缓存范围是:[-128,127]
总结发现char对应的包装数据类型缓存范围是:[0,127]
总结发现boolean对应的包装数据类型缓存的是:truefalse

Sysetem[了解]

含义

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class SystemDemo {

public static void main(String[] args) {
System.err.println("错误输出流");


Map<String,String> info = System.getenv();
for(String key : info.keySet()){
//System.out.println(key+"="+info.get(key));
System.out.println(key+"="+System.getenv(key));
}

//java native invoke
//System.loadLibrary(libname);
}
}

Runtime[了解]

含义

案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class RuntimeDemo {

public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();

try {
//执行操作系统的命令
runtime.exec("shutdown -a");
} catch (IOException e) {
e.printStackTrace();
}

try {
runtime.exec("mspaint");
} catch (IOException e) {
e.printStackTrace();
}
}
}

BigDecimal

含义

用来表示任意精度的小数,常用在金额、高精度仪器

注意点

常用方法

  • new BigDecimal(String value)

  • add

    加法

  • subtract

    减法

  • multiply

    乘法

  • divide

    除法

  • setScale

    四舍五入

案例

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
public class BigDecimalDemo {

//含义:精度可靠的浮点数
//常用的方法
//new BigDecimal(String value);

public static void main(String[] args) {

float data1 = 0.123456789f;

float result = data1*0.1f;

System.out.println(data1);
System.out.println(result);


BigDecimal one = new BigDecimal("0.123456789");
BigDecimal two = new BigDecimal("0.001");

BigDecimal three = one.add(two);

System.out.println(three.toString());

three = one.subtract(two);

three = one.divide(two);
System.out.println(three.toString());

three = one.multiply(two);
System.out.println(three.toString()); //0.000123456789

//常用四舍五入
System.out.println(three.setScale(7, BigDecimal.ROUND_HALF_UP).toString());
}
}

BigInteger

含义

无限大的整数

案例

TODO

DecimalFormat

含义

数字格式化

案例

TODO

集合

含义

容器,位于java.util包下面的类

数组特点

  • 大小固定[不会自动扩容]
  • 可以存基本数据、引用数据类型

集合特点

  • 大小不固定[自动扩容]
  • 只能存放引用数据类型

示意图

ArrayList

含义

是动态变长的数组

底层

是动态变长的数组

体系结构

  • Collection
    • List
      • ArrayList

常用方法

  • isEmpty
  • size
  • add
  • remove
  • set
  • contains
  • get
  • clear
  • toArray

案例

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
public class ArrayListDemo {

//集合
//含义:用来存放数据的容器,位于java.util包下的
//特点
//自动的扩容
//存放的都是引用数据类型

//ArrayList
//体系结构
//Collection
//List
//ArrayList
//本质
//可以伸缩的数组

//常用的方法
//isEmpty
//size
//add
//remove
//set
//contains
//get
//clear
//toArray
//...
public static void main(String[] args) {

ArrayList data = new ArrayList();

data.add(22);
data.add(33);

data.add(0, 11);

System.out.println(data.isEmpty()); //false

System.out.println(data.size()); //3

//更新
data.set(0,44);
data.set(0,11);


//List转成数组
System.out.println(Arrays.toString(data.toArray()));

//是否包含
System.out.println(data.contains(11));


//index element
//data.remove(11); //这是通过下标删除

//删除指定元素
data.remove(Integer.valueOf(11));

//获取
System.out.println(data.get(data.size()-1));

System.out.println(data);

data.clear();

System.out.println(data);
}
}

String的equals 跟equalsIgnoreCase区别?

1、使用equals( )方法比较两个字符串是否相等。它具有如下的一般形式:

boolean equals(Object str)

这里str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它返回true,否则返回false。这种比较是区分大小写的

2、为了执行忽略大小写的比较,可以调用equalsIgnoreCase( )方法。当比较两个字符串时,它会认为A-Z和a-z是一样的。其一般形式如下:

boolean equalsIgnoreCase(String str)

这里,str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度,它也返回true,否则返回false。