hashCode()
hashCode() 方法用于获取对象的 hash 值。
hashCode() 方法用于哈希查找,可以减少在查找中使用 equals() 的次数,重写了equals方法一般都要重写 hashCode() 方法。这个方法在一些具有哈希功能的 Collection 中用到。
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
System.out.println(obj1.hashCode()); // 460141958
System.out.println(obj2.hashCode()); // 1163157884
String str = new String();
System.out.println(str.hashCode()); // 0
ArrayList arr = new ArrayList<>();
System.out.println(arr.hashCode()); // 1
}
}
一般必须满足 obj1.equals(obj2) == true
。可以推出 obj1.hash Code() == obj2.hashCode()
,但是hashCode 相等不一定就满足 equals。不过为了提高效率,应该尽量使上面两个条件接近等价。
wait()
wait() 方法让当前线程进入等待状态。直到其他线程调用此对象的 notify() 方法或 notifyAll() 方法。
notify() 唤醒在该对象上等待的某个线程。
notifyAll() 唤醒在该对象上等待的所有线程。
import java.util.Date;
class WaitTest {
public static void main(String[] args) {
ThreadA threadA = new ThreadA(“threadA”);
synchronized (threadA) {
try {
// 启动线程
threadA.start();
System.out.println(Thread.currentThread().getName() + " wait() " + new Date());
// 主线程等待 ta 通过 notify 唤醒。
threadA.wait();// 不是使ta线程等待,而是当前执行 wait 的线程等待
System.out.println(Thread.currentThread().getName() + " continue " + new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class ThreadA extends Thread {
public ThreadA(String name) {
super(name);
}
@Override
public void run() {
synchronized (this) {
try {
Thread.sleep(1000); // 使当前线程阻塞1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " call notify()");
this.notify();
}
}
}
输出结果:
main wait() Sat Jul 24 13:03:57 CST 2021
threadA call notify()
main continue Sat Jul 24 13:03:58 CST 2021
finalize()
finalize() 方法用于实例被垃圾回收器回收的时触发的操作。
class Test {
public static void main(String[] args) {
User u1 = new User(1);
User u2 = new User(2);
User u3 = new User(3);
u2 = u3 = null;
System.gc();
}
}
class User {
private int id;
public User(int id) {
this.id = id;
System.out.println(“User Object " + id + " is created”);
}
protected void finalize() throws java.lang.Throwable {
super.finalize();
System.out.println(“User Object " + id + " is disposed”);
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
}
}
关于垃圾回收:
-
对象可能不被垃圾回收。只要程序没有濒临存储空间用完的那一刻,对象占用的空间就总也得不到释放。
-
垃圾回收并不等于 “析构”。
-
垃圾回收只与内存有关。使用垃圾回收的唯一原因是为了回收程序不再使用的内存。
finalize() 的用途:
无论对象是如何创建的,垃圾回收器都会负责释放对象占据的所有内存。
这就将对 finalize() 的需求限制到一种特殊情况,即通过某种创建对象方式以外的方式为对象分配了存储空间。不过这种情况一般发生在使用 “本地方法” 的情况下,本地方法是一种在 Java 中调用非 Java 代码的方式。
在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。
创建方式
class Test {
public static void main(String[] args) {
// 直接赋值方式创建对象是在方法区的常量池
String str1 = “hello”;
// 通过构造方法创建字符串对象是在堆内存
String str2 = new String(“hello”);
// 引用传递,str3 直接指向 st2 的堆内存地址
String str3 = str2;
String str4 = “hello”;
System.out.println(str1 == str2); // false
System.out.println(str1 == str3); // false
System.out.println(str3 == str2); // true
System.out.println(str1 == str4); // true
}
}
常用方法
字符串判断
| 方法 | 描述 |
| — | — |
| boolean equals(Object obj) | 比较字符串的内容是否相同 |
| boolean equalsIgnoreCase(String str) | 比较字符串的内容是否相同,忽略大小写 |
| boolean startsWith(String str) | 判断字符串对象是否以指定的str开头 |
| boolean endsWith(String str) | 判断字符串对象是否以指定的str结尾 |
字符串截取
| 方法 | 描述 |
| — | — |
| int length() | 获取字符串的长度,其实也就是字符个数 |
| char charAt(int index) | 获取指定索引处的字符 |
| int indexOf(String str) | 获取str在字符串对象中第一次出现的索引 |
| String substring(int start) | 从start开始截取字符串 |
| String substring(int start,int end) | 从start开始,到end结束截取字符串。包括start,不包括end |
字符串转换
| 方法 | 描述 |
| — | — |
| char[] toCharArray() | 把字符串转换为字符数组 |
| String toLowerCase() | 把字符串转换为小写字符串 |
| String toUpperCase() | 把字符串转换为大写字符串 |
其他方法
| 方法 | 描述 |
| — | — |
| String trim() | 去除字符串两端空格 |
| String[] split(String str) | 按照指定符号分割字符串 |
StringBuilder 是一个可变的字符序列。它继承于 AbstractStringBuilder,实现了 CharSequence 接口。
StringBuffer 也是继承于 AbstractStringBuilder 的子类;
但是,StringBuilder 和 StringBuffer 不同,前者是非线程安全的,后者是线程安全的。
常用方法
| 方法 | 描述 |
| — | — |
| public StringBuffer append(String s) | 将指定的字符串追加到此字符序列。 |
| public StringBuffer reverse() | 将此字符序列用其反转形式取代。 |
| public delete(int start, int end) | 移除此序列的子字符串中的字符。 |
| public insert(int offset, int i) | 将 int
参数的字符串表示形式插入此序列中。 |
| insert(int offset, String str) | 将 str
参数的字符串插入此序列中。 |
| replace(int start, int end, String str) | 使用给定 String
中的字符替换此序列的子字符串中的字符。 |
class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(10);
// StringBuffer sb = new StringBuffer(10);
sb.append(“Test…”);
System.out.println(sb); // Test…
sb.append("!");
System.out.println(sb); // Test…!
sb.insert(6, “Java”);
System.out.println(sb); // Test…Java!
sb.delete(4, 6);
System.out.println(sb); // TestJava!
}
}
StringBuilder 和 StringBuffer 用法差不多。
String、StringBuilder、StringBuffer 之间的区别:
首先需要说明的是:
-
String 字符串常量
-
StringBuilder 字符串变量(非线程安全)
-
StringBuffffer 字符串变量(线程安全)
在大多数情况下三者在执行速度方面的比较:StringBuilder > StringBuffer > String
对于三者使用的总结:
-
如果要操作少量的数据用 = String
-
单线程操作字符串缓冲区下操作大量数据 = StringBuilder
-
多线程操作字符串缓冲区下操作大量数据 = StringBuffer
所有的包装类(Integer、Long、Byte、Double、Float、Short)都是抽象类 Number 的子类。
包装类
| 包装类 | 基本数据类型 |
| — | — |
| Boolean | boolean |
| Byte | short |
| Short | short |
| Integer | int |
| Long | long |
| Character | char |
| Float | float |
| Double | double |
使用方法
实现 int 和 Integer 的相互转换
class Test {
public static void main(String[] args) {
int m = 500;
Integer obj = new Integer(m);
int n = obj.intValue();
System.out.println(n); // 500
Integer obj1 = new Integer(500);
System.out.println(obj.equals(obj1)); // true
}
}
将字符串转换为整数
class Test {
public static void main(String[] args) {
String[] str = {“123”, “123abc”, “abc123”, “abcxyz”};
for (String str1 : str) {
try {
int m = Integer.parseInt(str1, 10);
System.out.println(str1 + " 可以转换为整数 " + m);
} catch (Exception e) {
System.out.println(str1 + " 无法转换为整数");
}
}
}
}
输出结果:
123 可以转换为整数 123
123abc 无法转换为整数
abc123 无法转换为整数
abcxyz 无法转换为整数
将整数转换为字符串
class Test {
public static void main(String[] args) {
int m = 500;
String s = Integer.toString(m);
System.out.println(s); // 500
String s2 = m + “a”;
System.out.println(s2); // 500a
}
}
Java 的 Math 包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。
常量值
| 常量 | 描述 |
| — | — |
| Math.PI | 记录的圆周率 |
| Math.E | 记录e的常量 |
常用方法
| 方法 | 描述 |
| — | — |
| Math.abs | 返回参数的绝对值。 |
| Math.sin | 求指定double类型参数的正弦值。 |
| Math.cos | 求指定double类型参数的余弦值。 |
| Math.tan | 求指定double类型参数的正切值。 |
| Math.toDegrees | 将角度转换为弧度。 |
| Math.ceil | 得到不小于某数的最大整数。 |
| Math.floor | 得到不大于某数的最大整数。 |
| Math.IEEEremainder | 求余 |
| Math.max | 返回两个参数中的最大值。 |
| Math.min | 返回两个参数中的最小值。 |
| Math.sqrt | 求参数的算术平方根。 |
| Math.pow | 求某数的任意次方, 抛出ArithmeticException处理溢出异常 |
| Math.exp | 返回自然数底数e的参数次方。 |
| Math.log10 | 以10为底的对数 |
| Math.log | 返回参数的自然数底数的对数值。 |
| Math.rint | 求距离某数最近的整数(可能比某数大,也可能比它小)。 |
| Math.round | 同上,返回int型或者long型(上一个函数返回double型)。 |
| Math.random | 返回0,1之间的一个随机数。 |
random() 方法用于返回一个随机数,随机数范围为 0.0 =< Math.random < 1.0。
常用方法
| 方法 | 描述 |
| — | — |
| protected int next(int bits) | 生成下一个伪随机数。 |
| boolean nextBoolean() | 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的boolean值。 |
| void nextBytes(byte[] bytes) | 生成随机字节并将其置于用户提供的 byte 数组中。 |
| double nextDouble() | 返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布的double值。 |
| float nextFloat() | 返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布float值。 |
| double nextGaussian() | 返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正 态”)分布的double值,其平均值是0.0标准差是1.0。 |
| int nextInt() | 返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。 |
| int nextInt(int n) | 返回一个伪随机数,它是取自此随机数生成器序列的、在(包括和指定值(不包括)之间均匀分布的int值。 |
| long nextLong() | 返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。 |
| void setSeed(long seed) | 使用单个 long 种子设置此随机数生成器的种子。 |
import java.util.Random;