一、數據類型
1、Java的八種基本數據類型及其佔用的字節數。
public class DataType {
public static void main(String[] args) {
System.out.println("byte佔用字節數:" + Byte.BYTES);
System.out.println("short佔用字節數:" + Short.BYTES);
System.out.println("int佔用字節數:" + Integer.BYTES);
System.out.println("long佔用字節數:" + Long.BYTES);
System.out.println("float佔用字節數:" + Float.BYTES);
System.out.println("double佔用字節數:" + Double.BYTES);
System.out.println("char佔用字節數:" + Character.BYTES);
System.out.println("boolean佔用字節數:不固定");
}
}
2、Java中的自動裝箱、拆箱。
public class AutoBoxing {
public static void main(String[] args) {
Integer a = 1; // 自動裝箱
int b = a; // 自動拆箱
System.out.println(a + b);
}
}
3、字符串的不可變性以及String、StringBuilder、StringBuffer的區別。
public class StringDemo {
public static void main(String[] args) {
String a = "str";
// String不可變性示例
a = a + "ing";
System.out.println(a);
// StringBuilder、StringBuffer示例
StringBuilder sb = new StringBuilder("str");
sb.append("ing");
System.out.println(sb.toString());
}
}
二、集合
1、ArrayList、LinkedList、Vector的區別。
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
public class ListDemo {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
LinkedList linkedList = new LinkedList();
Vector vector = new Vector();
for (int i = 0; i < 1000000; i++) {
arrayList.add(i);
linkedList.add(i);
vector.add(i);
}
long start = System.nanoTime();
for (int i = 0; i < arrayList.size(); i++) {
arrayList.get(i);
}
System.out.println("ArrayList遍歷時間:" + (System.nanoTime() - start) + "ns");
start = System.nanoTime();
for (int i = 0; i < linkedList.size(); i++) {
linkedList.get(i);
}
System.out.println("LinkedList遍歷時間:" + (System.nanoTime() - start) + "ns");
start = System.nanoTime();
for (int i = 0; i < vector.size(); i++) {
vector.get(i);
}
System.out.println("Vector遍歷時間:" + (System.nanoTime() - start) + "ns");
}
}
2、HashMap、ConcurrentHashMap的區別。
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class MapDemo {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap();
for (int i = 0; i < 1000000; i++) {
hashMap.put(i, i);
concurrentHashMap.put(i, i);
}
long start = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
hashMap.get(i);
}
System.out.println("HashMap遍歷時間:" + (System.nanoTime() - start) + "ns");
start = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
concurrentHashMap.get(i);
}
System.out.println("ConcurrentHashMap遍歷時間:" + (System.nanoTime() - start) + "ns");
}
}
3、集合的容器底層原理。
由於不同容器底層實現方式不同,這裡就不一一列舉了。
三、並發與多線程
1、Thread和Runnable的區別。
public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("繼承Thread類創建線程");
}
}
public class RunnableDemo implements Runnable {
@Override
public void run() {
System.out.println("實現Runnable接口創建線程");
}
}
public class Main {
public static void main(String[] args) {
ThreadDemo thread = new ThreadDemo();
thread.start();
RunnableDemo runnable = new RunnableDemo();
new Thread(runnable).start();
}
}
2、wait和sleep的區別。
public class WaitSleepDemo {
private static final Object lock = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + "獲取到鎖");
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "獲取到了鎖,執行結束");
}
}, "wait").start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + "獲取到鎖");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.notifyAll();
System.out.println(Thread.currentThread().getName() + "執行notify操作");
}
}, "sleep").start();
}
}
3、線程死鎖及解決方案。
關於線程死鎖的問題,一般可以採用如下策略進行解決: 1、避免一個線程同時獲得多個鎖。 2、避免一個線程在等待另外一個線程持有的鎖時,自身抱持有的鎖不釋放。 3、使用定時鎖,使用Lock.tryLock(timeout)來替代使用內部鎖機制。 4、對於數據庫鎖,加鎖和解鎖要在同一個連接中完成。
四、JVM
1、JVM的內存結構及垃圾回收算法。
由於JVM內存結構較為複雜,這裡只列舉常見的垃圾回收算法:標記-清除算法、複製算法、標記-整理算法、分代收集算法。
2、Java內存模型及synchronized的實現原理。
public class SynchronizedDemo {
public synchronized void method() {
System.out.println("synchronized方法");
}
public void block() {
synchronized (this) {
System.out.println("synchronized代碼塊");
}
}
}
3、性能調優相關選項和工具,比如jconsole、jstack等。
在開發中,經常會遇到一些性能問題,這時可以使用jconsole、jstack等工具進行診斷。其中,jconsole主要用於監控和管理JVM,jstack主要用於查看Java虛擬機進程中的線程堆棧信息。
五、設計模式
1、常見的23種設計模式及應用場景。
這裡不一一展開,可以參考《Head First設計模式》和《GOF設計模式》。
2、單例模式的實現。
public class Singleton {
private static Singleton instance = null;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
3、工廠模式、抽象工廠模式的區別及應用場景。
工廠模式是最常用的設計模式之一,它提供了一種創建對象的方式,並且可以靈活地根據需要來選擇實現類,同時也可以解決依賴關係。抽象工廠模式則是在工廠模式的基礎上增加了一層抽象,通過接口的方式來解決實現類的迭代問題。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/300376.html
微信掃一掃
支付寶掃一掃