Object
所有类的父类
-
Class getClass()
通过对象的该方法,可以获得对象对应的类的字节码对象 -
Object clone() - 克隆、赋值
调用方法的对象, 需要类实现Cloneable接口
深克隆:复制一份全新的, 属性内容一致的对象
浅克隆:复制的是引用 -
wait() - 与线程有关
(有三个) -
notify() / notifyAll() - 与线程有关
-
void finalize()
清理内存时自动调用
GC: 垃圾回收机制, 定时清理内存
也可以手动调用: System.gc() 清理内存 -
hashCode()
获得的是地址 哈希码
(重点)
7. String toString()
直接打印对象时, 自动就会调用对象的toString方法
Object中默认toString的实现方式:(地址)
getClass().getName() + “@” + Integer.toHexString(hashCode());
ZiToString zi = new ZiToString("张三", 28);
// com.zzxx.api.Object.ZiToString@5fe5c6f (地址)
System.out.println(zi);
System.out.println(zi + "");
System.out.println(zi.toString());
// 想要打印对象时, 显示对象的属性内容 -- 重写toString方法
System.out.println(zi); // 张三,28
// 重写的 toString()方法
public String toString() {
return name + "," + age;
}
// 默认的toString重写方法
@Override
public String toString() {
return "S{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
- boolean equals(Object o)
Object中默认toString的实现方式:(比较地址)
getClass().getName() + “@” + Integer.toHexString(hashCode());
// 重写 equals 方法
public boolean equals(Object obj) {
// this-ZiEquals 和 obj-Object
// 1.this == obj, 一定相等
if (this == obj) {
return true;
}
// 2.类型不一致,一定是不等
if (!(obj instanceof ZiEquals)) {
return false;
}
// 3.类型一致,不是同一个对象,才要比较属性内容(这样写有个空值的问题)
ZiEquals zi = (ZiEquals) obj;
return zi.age == this.age && this.name.equals(zi.name);
}
// 默认的重写方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ZiEquals ziEquals = (ZiEquals) o;
// Objects.equals 方法里面判断了空值的问题
return age == ziEquals.age &&
Objects.equals(name, ziEquals.name);
}
Date
java.util.Date - 时间、日期
构造方法
// 空参构造方法 -> 当前系统时间
Date date = new Date();
// Tue Jul 21 11:48:57 CST 2020
System.out.println(date);
// 参数是long类型的构造方法
// 注意的是要加 l(第一个数之后加l 避免数值越界)
// 表示 1个月后的时间
Date date1 = new Date(0l + 30l*24*60*60*1000);
常用的API - (getTime、 setTime)
Date date = new Date();
// 将时间从date类型, 转换成了long类型所表示的毫秒值
long time = date.getTime();
System.out.println("time: " + time);
// 一个月以后的date时间
time += 30l*24*60*60*1000;
date.setTime(time);
System.out.println(date);
Date date1 = new Date(0l);
// date1 是否在 date 的后面
boolean b = date1.after(date);
// date1 是否在 date 的前面
boolean b2 = date1.before(date);
Calendar
java.util.Calendar - 万年历
构造方法
// 获得万年历(Calendar是抽象类,调用getInstance()方法)
Calendar cal = Calendar.getInstance();
System.out.println(cal);
常用的API - (get、set、add)
// 1.获得万年历对象
Calendar cal = Calendar.getInstance();
System.out.println(cal);
// 2.get set
// 1). 获得cal对应的月份, 月份从0开始
int month = cal.get(Calendar.MONTH);
System.out.println("month: " + month); // 6
// 2). 获得cal对应的年份
int year = cal.get(Calendar.YEAR);
System.out.println("year: " + year); // 2020
// DATE DAY_OF_MONTH
// HOUR MINUTE SECOND DAY_OF_WEEK
// HOUR_OF_DAY
// 3) 今天时间是:2020-7-21 ->设置为2020-8-15
cal.set(Calendar.MONTH, Calendar.AUGUST);
cal.set(Calendar.DAY_OF_MONTH, 15);
// 当加的内容结果跨月\年\星期, 自动进行推算
// 生产日期今天,保质期48天 保质期的前两周的星期四搞促销
cal.add(Calendar.DATE, 48);
// 到期日往前推两周
cal.add(Calendar.WEEK_OF_MONTH, -2);
// 要周四
cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
Date、long、Calendar之间的转换(重点)
// 1.Date -> long
Date date = new Date();
long time = date.getTime();
// 2.long -> Date
date.setTime(time);
// 如果原先没有Date对象, 可以通过long创建一个新的Date对象
Date date2 = new Date(time);
// 3.Calendar -> Date
Calendar cal = Calendar.getInstance();
Date date3 = cal.getTime();
// 4.Date -> Calendar
Date date4 = new Date(0);
cal.setTime(date4);
// 5.Calendar -> long (了解)
Calendar cal1 = Calendar.getInstance();
long time2 = cal1.getTimeInMillis();
// 6.long -> Calendar (了解)
cal1.setTimeInMillis(time2 + 1000);
DateFormat
java.text.DateFormat - 自定义格式
子类SimpleDateFormat
// 为了显示时间 Date -> String
Date date = new Date();
System.out.println(date);
// 构造SimpleDateFormat对象, 需要传参 - 格式
// 2020-07-21 16:47:55
// yyyy-MM-dd HH:mm:ss E
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String str = sf.format(date);
System.out.println(str);
// 自行输入日期(String) -> Date 19980228
String s = "19980228";
SimpleDateFormat sf1 = new SimpleDateFormat("yyyyMMdd");
Date date1 = sf1.parse(s);
System.out.println(sf.format(date1));
String
正则表达式
字符串的一些排列规则
开始符号: ^
结束符号: $
-
常用标识
[ ]:表示一个字符的位置
例:
[abc] -> 这个字符的位置内容只能是a或者b或者c
[a-z] -> 这个字符的位置内容可以是所有的小写字母 -
数量词
+:出现一次或多次
?: 一次或者0次
*:0次或者多次
{n}: 刚好n次
{n,m}: n~m次
{n,}:至少n次 -
特殊
例:^abc$ -> “abc” -
预定义字符类
. : 所有字符
\d : 数字 [0-9]
\s : 空白字符:[ \t\n\x0B\f\r]
\w : 单词字符:[a-zA-Z_0-9]
\D : 非数字 [^0-9] -
转义: 有特殊意义的字符
表示字符本身, 需要 \字符
\. \\ \+ \? \* \-
特殊:([ ]中出现的特殊字符, 不需要转移, 就代表字符本身)
常用API - (matches、replaceAll、split)
// matchs
String str = "ssseeeww@xxx.com.cn.edu.cc";
// \w{6,18}@\w{3,8}(\.\w{2,6})+
String regex = "^\\w{6,18}@\\w{3,8}(\\.\\w{2,6})+$";
boolean m = str.matches(regex);
// replaceAll
// (sb|nc|qnm|qnmlgb|nmb)+
String regex = "(sb|nc|qnm|qnmlgb|nmb)+";
String word = "你怎么回事, 真是个sb, 你这个nc, qnm, 会不会玩!";
word = word.replaceAll(regex, "**");
System.out.println(word);
// split
String str = "string123date14hello4word34234";
// 需求: string date hello word
String regex = "[0-9]+";
String[] words = str.split(regex);
System.out.println(Arrays.toString(words)); // [string, date, hello, word]
乱码问题
JVM -> 1个char = 2个byte (Unicode编码)
操作系统:GBK、UTF-8、ISO8859-1
String str = "你好";
// 字符 -> 字节 (编码)
byte[] bs = str.getBytes();
// [-28, -67, -96, -27, -91, -67] UTF-8
System.out.println(Arrays.toString(bs));
// [-60, -29, -70, -61] GBK
byte[] bs1 = str.getBytes("GBK");
System.out.println(Arrays.toString(bs1));
// 字节 -> 字符 (解码)
String s1 = new String(bs);
System.out.println("s1: " + s1);
String s2 = new String(bs1, "gbk");
System.out.println("s2: " + s2);
// 手动制造乱码
String str = "你好";
byte[] bs = str.getBytes();
String s = new String(bs, "ISO8859-1");
System.out.println(s); // ä½ å¥½
// 将 s 的乱码解决
byte[] b = s.getBytes("ISO8859-1");
s = new String(b, "UTF-8");
System.out.println(s);
// 简化成一句话
s = new String(s.getBytes("ISO8859-1"), "UTF-8");
System.out.println(s);
StringBuilder
可变字符串
StringBuffer : 线程安全的
StringBuilder: 效率高
构造方法
// 无参构造方法
StringBuilder sb = new StringBuilder();
// 有参构造方法
StringBuilder sb1 = new StringBuilder("haha");
常用API - (append、delete、replace、insert、 reverse)
StringBuilder sb = new StringBuilder();
// 字符串拼接 - 末尾追加
sb.append("haha");
sb.append("heihei");
System.out.println(sb); // hahaheihei
// 在指定位置插入字符串
sb.insert(4, "xixi");
System.out.println(sb); // hahaxixiheihei
// 将start 和end 中间的字符串替换成指定字符串
sb.replace(0, 4, "hehe");
System.out.println(sb); // hehexixiheihei
// 删除start 到 end之间的字符串内容
sb.delete(0, 4);
System.out.println(sb); // xixiheihei
// 字符串反转
sb.reverse();
System.out.println(sb); // iehiehixix
// 将StringBuilder变成String
String str = sb.toString();
包装类
将基本数据类型做了封装
byte -> Byte
short -> Short
int -> Integer
long -> Long
double -> Double
float -> Float
char -> Character
boolean -> Boolean
Integer常用API
跟String一样有常量池
// 1.基本数据类型和包装类型之间可以直接转换
int i = 1;
// 2.包装类型比基本数据类型多表示一个null值
Integer i1 = 2; // null
// 3.包装类型除了可以直接赋值, 还可以new对象
i1 = new Integer(2);
// 1.1 int -> Integer 隐藏了自动装箱的操作
i1 = i; // 等同于: i1 = Integer.valueOf(i);
// 1.2 Integer -> int 隐藏了自动拆箱的操作
i = i1; // 等同于: i = i1.intValue();
// 4.字符串 可以转换成 包装类型
Integer i3 = Integer.valueOf("12");
// 将字符串 转换成基本数据类型(16进制)
int i4 = Integer.parseInt("ff", 16);
//int i5 = Integer.parseInt("12a"); // (默认10进制)会发生异常 NumberFormatException
// 5.整数类型, 类中定义了最大值和最小值字段
System.out.println("int的最大值:" + Integer.MAX_VALUE);
System.out.println("byte的最小值:" + Byte.MIN_VALUE);
Integer i1 = 100;
Integer i2 = 100;
Integer i3 = new Integer(100);
// 结论1: Integer也有常量池
System.out.println(i1 == i2); // true
i1 = 128;
i2 = 128;
// 结论2: 常量池的范围: -128 ~ 127
System.out.println(i1 == i2); // false
Integer i4 = Integer.valueOf(127);
Integer i5 = Integer.valueOf(127);
System.out.println(i4 == i5); // true