基本类型与包装类型的拆箱与装箱
1 2 3 4 5 6 7 8 9 10 11
| int primitiveInt = 42; Integer boxedInt = primitiveInt;
boxedInt = 42; primitiveInt = boxedInt;
primitiveInt = 42; boxedInt = Integer.valueOf(primitiveInt);
boxedInt = 42; primitiveInt = boxedInt.intValue();
|
查看编译后class字节码文件。
并发编程
面试重灾区。
多线程
如何创建一个线程?
线程池的使用?线程池的参数配置。
锁
synchronized 的锁升级过程?
synchronized 与 lock的区别?
枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
enum Season { SPRING("Warm"), SUMMER("Hot"), AUTUMN("Cool"), WINTER("Cold");
private final String description;
Season(String description) { this.description = description; }
public String getDescription() { return description; } }
|
1 2 3
| for (Day day : Day.values()) { System.out.println(day); }
|
1 2 3
| Day day = Day.MONDAY; String name = day.name(); int ordinal = day.ordinal();
|
1 2 3 4
| Day today = Day.MONDAY; if (today == Day.MONDAY) { System.out.println("It's Monday!"); }
|
1 2 3 4 5 6 7
| Day day = Day.MONDAY; switch (day) { case MONDAY: System.out.println("It's Monday!"); break; }
|
泛型
泛型类
1 2 3 4 5 6 7 8 9 10 11
| public class Box<T> { private T value;
public T getValue() { return value; }
public void setValue(T value) { this.value = value; } }
|
泛型方法:
1 2 3 4
| public <T> T genericMethod(T input) { return input; }
|
1 2 3
| Box<Integer> intBox = new Box<>(); intBox.setValue(42); Integer value = intBox.getValue();
|
1 2 3
| public void printBox(Box<?> box) { System.out.println(box.getValue()); }
|
1 2 3 4 5 6 7
| public <T extends Number> void processNumbers(List<T> numbers) { }
public void addIntegers(List<? super Integer> list) { list.add(42); }
|
泛型擦除
1 2 3 4
| List<String> stringList = new ArrayList<>(); List<Integer> intList = new ArrayList<>();
System.out.println(stringList.getClass() == intList.getClass());
|
异常处理
开发代码时如何正确处理和抛出异常。
开发代码时一般都会自定义异常。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class BusinessException extends RuntimeException {
private final String errorCode;
public BusinessException(String message, String errorCode) { super(message); this.errorCode = errorCode; }
public BusinessException(String message, Throwable cause, String errorCode) { super(message, cause); this.errorCode = errorCode; }
public String getErrorCode() { return errorCode; } }
|
面试题
常见的异常类有哪些?
什么是受检异常、什么是非受检异常?
JDBC
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
| import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
public class JdbcExample {
public static void main(String[] args) { String jdbcUrl = "jdbc:mysql://localhost:3306/test"; String user = "your_username"; String password = "your_password";
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
try { Class.forName(jdbcDriver); } catch (ClassNotFoundException e) { System.err.println("无法加载数据库驱动: " + e.getMessage()); return; }
try (Connection connection = DriverManager.getConnection(jdbcUrl, user, password)) { String sql = "SELECT * FROM test"; try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name); } } } catch (SQLException e) { System.err.println("数据库操作错误: " + e.getMessage()); } } }
|
反射
获取class对象:
1 2 3 4 5 6 7 8 9
| Class<?> myClass1 = Class.forName("com.example.MyClass");
Class<?> myClass2 = MyClass.class;
MyClass myInstance = new MyClass(); Class<?> myClass3 = myInstance.getClass();
|
创建实例:
1 2
| Class<?> myClass = Class.forName("com.example.MyClass"); Object myInstance = myClass.newInstance();
|
获取方法:
1 2 3 4 5 6 7 8 9 10
| Class<?> myClass = Class.forName("com.example.MyClass");
Method[] methods = myClass.getDeclaredMethods();
Method myMethod = myClass.getDeclaredMethod("myMethod", String.class);
Object result = myMethod.invoke(myInstance, "parameter");
|
获取字段:
1 2 3 4 5 6 7 8 9 10 11
| Class<?> myClass = Class.forName("com.example.MyClass");
Field[] fields = myClass.getDeclaredFields();
Field myField = myClass.getDeclaredField("myField");
myField.setAccessible(true); Object value = myField.get(myInstance);
|
注解
内建注解
jdk内置的。
1 2 3
| @Override:用于标记一个方法覆盖父类的方法。 @Deprecated:用于标记已过时的方法、类或字段。 @SuppressWarnings:用于抑制编译器产生的警告。
|
元注解
元注解是用于注解其他注解的注解,提供了更多关于注解本身的信息。
1 2 3
| @Retention:用于指定注解的保留策略,有三个取值:SOURCE、CLASS、RUNTIME @Target:用于指定注解可以应用的程序元素类型,如类、方法、字段等。 @Documented:用于指定注解是否包含在 Java 文档中。
|
自定义注解
1 2 3 4 5 6 7 8 9 10 11
| import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface MyAnnotation { String value() default "default value"; int count() default 1; }
|
1 2 3 4 5 6 7 8
| @MyAnnotation(value = "Custom Annotation", count = 5) public class MyClass {
@MyAnnotation(count = 10) public void myMethod() { } }
|