遵命!机母大人免安装绿色中文版
15.5G · 2025-11-08
面向对象是利用语言对现实事物进行抽象。面向对象具有以下特征:
深拷贝和浅拷贝就是指对象的拷贝,一个对象中存在两种类型的属性,一种是基本数据类型,一种是实例对象的引用。
| 方法 | 定义 | 常见用途 |
|---|---|---|
wait() | 释放锁并进入等待状态,直到被其他线程 notify() 或 notifyAll() 唤醒 | 线程间协作,等待某个条件的发生 |
sleep() | 暂停当前线程一段时间,但不释放锁 | 模拟延迟,控制线程执行节奏 |
| 特性 | wait() | sleep() |
|---|---|---|
| 锁释放 | 释放当前对象锁 | 不释放锁 |
| 调用位置 | 必须在同步块 (synchronized) 中调用 | 可以在任何地方调用 |
| 所属类 | Object 类 | Thread 类 |
| 中断行为 | 会抛出 InterruptedException | 会抛出 InterruptedException |
| 唤醒机制 | 需要 notify() 或 notifyAll() | 自动恢复,无需唤醒 |
| 状态 | 进入 WAITING 或 TIMED_WAITING 状态 | 进入 TIMED_WAITING 状态 |
自动装箱(Autoboxing) 和 自动拆箱(Unboxing) 是 Java 为了简化基本类型和包装类型之间的转换而引入的机制。java为什么要引入自动装箱和拆箱的功能?主要是用于java集合中,List<Inteter> list=new ArrayList<Integer>();list集合如果要放整数的话,只能放对象,不能放基本类型,因此需要将整数自动装箱成对象。
int primitiveInt = 10; Integer wrappedInt = primitiveInt;Integer wrappedInt = 20; int primitiveInt = wrappedInt;| 特性 | int | Integer |
|---|---|---|
| 类型 | 基本数据类型 | 包装类(java.lang.Integer) |
| 内存分配 | 栈内存 | 堆内存 |
| 初始值 | 0 | null |
| 存储范围 | -2^31 到 2^31-1 | -2^31 到 2^31-1 |
| 性能 | 更高 | 较低 |
| 方法 | 无 | 包含常用方法,如 parseInt()、toString() |
| 比较 | == 比较值 | == 比较引用,.equals() 比较值 |
Integer 在 -128 到 127 之间的值会 缓存,在这个范围内的 Integer 对象会复用已有实例,超出这个范围的值则会创建新的实例。
| 特性 | == | equals() |
|---|---|---|
| 比较内容 | 比较引用地址(基本类型比较值) | 默认比较引用地址,可重写后比较值 |
| 适用类型 | 基本数据类型和引用类型 | 仅适用于引用类型 |
| 性能 | 更快 | 较慢(涉及方法调用) |
| 可重写性 | 不可重写 | 可在类中重写 |
| 空指针安全 | 安全 | 可能抛出 NullPointerException |
虽然 String 是引用类型,但在某些情况下可以直接使用 == 进行比较,这是因为 String 具有常量池机制。但String还是建议使用.equals()去比较字符串,也可使用Objects.equals()避免空指针异常。
| 特性 | StringBuffer | StringBuilder |
|---|---|---|
| 线程安全性 | 线程安全(方法同步) | 非线程安全 |
| 性能 | 较慢(同步开销) | 较快 |
| 适用场景 | 多线程环境 | 单线程环境 |
| 初始版本 | JDK 1.0 | JDK 1.5 |
| 常用方法 | append(), insert(), delete(), reverse() | append(), insert(), delete(), reverse() |
| 是否可变 | 可变 | 可变 |
protected Object clone()——创建并返回此对象的一个副本。boolean equals(Object obj)——指示某个其它对象是否与此对象相等。protected void finalize()——当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。Class<? extends Object> getClass()——返回一个对象的运行时类。int hashCode()——返回该对象的哈希码值。void notify()——唤醒在此对象监视器上等待的单个线程。void notifyAll()——唤醒在此对象监视器上等待的所有线程。String toString()——返回该对象的字符串表示。java.util.Collection (顶层接口)
├── java.util.List(有序、可重复)
│ ├── java.util.ArrayList
│ ├── java.util.LinkedList
│ └── java.util.Vector
│ └── java.util.Stack
├── java.util.Set(无序、不可重复)
│ ├── java.util.HashSet
│ ├── java.util.LinkedHashSet
│ └── java.util.TreeSet
└── java.util.Queue(队列,FIFO)
├── java.util.PriorityQueue
└── java.util.Deque(双端队列)
├── java.util.LinkedList
└── java.util.ArrayDeque
java.util.Map(键值对)
├── java.util.HashMap
├── java.util.LinkedHashMap
├── java.util.TreeMap
└── java.util.Hashtable
AI写代码mathematica
123456789101112131415161718192021
HashMap (JDK 8+)
├── 数组(Node<K, V>[])
│ ├── 链表
│ └── 红黑树
└── 散列函数(hash())
AI写代码scss
12345
数组:查询效率高,O(1)时间复杂度;
链表:处理哈希冲突,适合小规模冲突;
红黑树:当链表长度超过8时,转换为红黑树,提高查询效率。
static final int hash(Object key) {
int h;
//异或高位扰动,减少哈希冲突
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
AI写代码java
运行
12345
| 特性 | HashMap | Hashtable |
|---|---|---|
| 线程安全 | 非线程安全 | 线程安全 |
| 效率 | 高 | 低 |
| Null 键和值 | 允许 1 个 null 键,多个 null 值 | 不允许 null 键和 null 值 |
| 初始容量 | 16 | 11 |
| 容量要求 | 容量一定是2的整数幂 | 不一定是2的整数幂 |
| 扩容机制 | 容量翻倍 | 容量翻倍 + 1 |
| 负载因子 | 0.75(默认) | 0.75(固定) |
| 遍历方式 | Iterator | Enumerator |
| 底层实现 | 数组 + 链表 + 红黑树 | 数组 + 链表 |
| JDK 版本 | JDK 1.2 | JDK 1.0 |
| 推荐使用 | 大部分场景 | 兼容旧代码 |
Thread 类,重写 Thread 类的 run() 方法,通过 start() 方法启动线程// 继承 Thread 实现线程
class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
thread1.start();
thread2.start();
}
}
AI写代码java
运行
12345678910111213141516171819
2. 实现 Runnable 接口,实现 Runnable 接口的 run() 方法,通过 Thread 构造函数传入 Runnable 实例
// 实现 Runnable 接口
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
}
}
}
public class RunnableTest {
public static void main(String[] args) {
Thread thread1 = new Thread(new MyRunnable());
Thread thread2 = new Thread(new MyRunnable());
thread1.start();
thread2.start();
}
}
AI写代码java
运行
1234567891011121314151617181920
3. 实现 Callable 接口(带返回值),实现 Callable 接口的 call() 方法,使用 FutureTask 包装 Callable 实例,通过 Thread 启动
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return Thread.currentThread().getName() + " - Task Completed";
}
}
public class CallableTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<String> task = new FutureTask<>(new MyCallable());
Thread thread = new Thread(task);
thread.start();
// 阻塞等待线程执行完毕
String result = task.get();
System.out.println(result);
}
}
AI写代码java
运行
1234567891011121314151617181920212223
4. 使用线程池(ExecutorService),使用 Executors 创建线程池,提交任务 (submit() / execute())
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int i = 0; i < 5; i++) {
executor.execute(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
});
}
executor.shutdown();
}
}
AI写代码java
运行
12345678910111213141516
5. 使用匿名内部类
public class AnonymousThreadTest {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
}
});
thread.start();
}
}
AI写代码java
运行
1234567891011
6. 使用Lambda表达式
public class LambdaThreadTest {
public static void main(String[] args) {
new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - Count: " + i);
}
}).start();
}
}
AI写代码java
运行
12345678910
| 方式 | 优点 | 缺点 |
|---|---|---|
| 继承 Thread | 代码简单,易于理解 | 不能继承其他类 |
| 实现 Runnable | 可实现多继承,解耦逻辑 | 无返回值 |
| 实现 Callable | 支持返回值和异常处理 | 代码稍复杂 |
| 线程池 | 高效管理线程,复用线程资源 | 需要手动关闭线程池 |
| 匿名内部类 | 简化代码结构 | 可读性较差 |
| Lambda | 简洁、高效 | 只能用于函数式接口 |