Java面试宝典:Java基础核心知识点详解

一、基本数据类型

1.1 基本数据类型分类

Java有8种基本数据类型:

// 数值型
byte     -128 ~ 127
short    -32768 ~ 32767
int      -2^31 ~ 2^31-1
long     -2^63 ~ 2^63-1
float    单精度浮点数
double   双精度浮点数

// 字符型
char     单个字符,Unicode编码

// 布尔型
boolean  true 或 false

1.2 基本数据类型与包装类的关系

// 基本类型和包装类
byte    Byte
short   Short
int     Integer
long    Long
float   Float
double  Double
char    Character
boolean Boolean

1.3 基本数据类型默认值

public class DefaultValues {
    byte b;
    short s;
    int i;
    long l;
    float f;
    double d;
    char c; // '\u0000'
    boolean bool; // false
    
    public static void main(String[] args) {
        DefaultValues dv = new DefaultValues();
        System.out.println("byte: " + dv.b);
        System.out.println("short: " + dv.s);
        System.out.println("int: " + dv.i);
        System.out.println("long: " + dv.l);
        System.out.println("float: " + dv.f);
        System.out.println("double: " + dv.d);
        System.out.println("char: " + dv.c);
        System.out.println("boolean: " + dv.bool);
    }
}

二、String原理

2.1 String不可变性

String s1 = "hello";
String s2 = s1.concat("world");
// s1 仍然是 "hello"
// s2 是 "helloworld"

// String不可变的证明
public class StringImmutability {
    public static void main(String[] args) {
        String str = "Hello";
        String newStr = str.concat(" World");
        System.out.println("str: " + str); // Hello
        System.out.println("newStr: " + newStr); // Hello World
    }
}

2.2 String内存存储

String对象存储在堆内存中,使用常量池管理。

String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String(s1);

System.out.println(s1 == s2); // true (常量池)
System.out.println(s1 == s3); // false
System.out.println(s1 == s4); // false
System.out.println(s3 == s4); // false

// intern()方法
String s5 = new String("xyz").intern();
String s6 = "xyz";
System.out.println(s5 == s6); // true

2.3 String常用方法

public class StringMethods {
    public static void main(String[] args) {
        String str = "Hello World";
        
        // 长度
        System.out.println(str.length()); // 11
        
        // 字符
        System.out.println(str.charAt(0)); // H
        
        // 子串
        System.out.println(str.substring(0, 5)); // Hello
        
        // 比较
        System.out.println(str.compareTo("Hello Java")); // 负数
        System.out.println(str.equals("Hello World")); // true
        
        // 查找
        System.out.println(str.indexOf('W')); // 6
        System.out.println(str.lastIndexOf('l')); // 9
        
        // 替换
        System.out.println(str.replace("World", "Java")); // Hello Java
        
        // 分割
        String[] arr = str.split(" "); // [Hello, World]
        
        // 去空格
        System.out.println("  abc  ".trim()); // abc
        
        // 大小写
        System.out.println(str.toUpperCase()); // HELLO WORLD
        System.out.println(str.toLowerCase()); // hello world
    }
}

三、equals与==的区别

3.1 ==和equals的区别

public class EqualsVsEqual {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("abc");
        String s3 = "abc";
        String s4 = "abc";
        
        // == 比较的是内存地址
        System.out.println(s1 == s2); // false
        System.out.println(s3 == s4); // true (常量池)
        System.out.println(s1 == s3); // false
        
        // equals 比较的是内容
        System.out.println(s1.equals(s2)); // true
        System.out.println(s3.equals(s4)); // true
        System.out.println(s1.equals(s3)); // true
        
        // 包装类比较
        Integer i1 = new Integer(100);
        Integer i2 = new Integer(100);
        System.out.println(i1 == i2); // false
        System.out.println(i1.equals(i2)); // true
    }
}

3.2 重写equals方法

public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Person person = (Person) obj;
        return age == person.age && Objects.equals(name, person.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

四、包装类详解

4.1 自动装箱与拆箱

public class BoxingUnboxing {
    public static void main(String[] args) {
        // 自动装箱
        Integer i1 = 100; // 自动转为 Integer.valueOf(100)
        Integer i2 = Integer.valueOf(200);
        
        // 自动拆箱
        int i3 = i1; // 自动转为 i1.intValue()
        int i4 = i2;
        
        // 比较包装类
        System.out.println(i1 == i3); // true
        System.out.println(i1 == i2); // false (不同的对象)
        
        // 缓存机制
        Integer a = 100;
        Integer b = 100;
        Integer c = 200;
        Integer d = 200;
        System.out.println(a == b); // true (使用缓存)
        System.out.println(c == d); // false (超出缓存范围)
    }
}

4.2 Integer缓存

public class IntegerCache {
    public static void main(String[] args) {
        // -128 到 127 之间的整数会被缓存
        Integer a = 127;
        Integer b = 127;
        Integer c = 128;
        Integer d = 128;
        
        System.out.println(a == b); // true
        System.out.println(c == d); // false
        
        // 使用 Integer.valueOf()
        Integer e = Integer.valueOf(100);
        Integer f = Integer.valueOf(100);
        System.out.println(e == f); // true
        
        // 使用 new 关键字
        Integer g = new Integer(100);
        Integer h = new Integer(100);
        System.out.println(g == h); // false
    }
}

五、数据类型转换

5.1 基本类型转换

public class TypeConversion {
    public static void main(String[] args) {
        int a = 10;
        long b = a; // 自动类型提升
        float c = b; // 自动类型提升
        
        // 强制类型转换
        int d = (int) c;
        
        // 基本类型与包装类转换
        int e = Integer.parseInt("100");
        double f = Double.parseDouble("3.14");
        
        // 包装类与基本类型转换
        int g = Integer.valueOf("100");
        String s = String.valueOf(100);
    }
}

5.2 String与其他类型转换

public class StringConversion {
    public static void main(String[] args) {
        // String转基本类型
        int a = Integer.parseInt("100");
        double b = Double.parseDouble("3.14");
        boolean c = Boolean.parseBoolean("true");
        
        // 基本类型转String
        String d = String.valueOf(100);
        String e = Integer.toString(100);
        String f = String.format("%d", 100);
        
        // 格式化输出
        String g = String.format("%.2f", 3.14159); // 3.14
        String h = String.format("%d-%02d-%02d", 2024, 1, 1); // 2024-01-01
    }
}

六、运算符详解

6.1 算术运算符

public class ArithmeticOperators {
    public static void main(String[] args) {
        int a = 10, b = 3;
        
        System.out.println(a + b); // 加法
        System.out.println(a - b); // 减法
        System.out.println(a * b); // 乘法
        System.out.println(a / b); // 除法 (3)
        System.out.println(a % b); // 取余 (1)
        
        // 自增自减
        int c = a++;
        int d = ++b;
        
        // 短路运算
        boolean e = true && false; // false
        boolean f = true || false; // true
    }
}

6.2 比较运算符

public class ComparisonOperators {
    public static void main(String[] args) {
        int a = 10, b = 20;
        
        System.out.println(a == b); // false
        System.out.println(a != b); // true
        System.out.println(a > b);  // false
        System.out.println(a < b);  // true
        System.out.println(a >= b); // false
        System.out.println(a <= b); // true
    }
}

七、流程控制

7.1 if-else语句

public class IfElse {
    public static void main(String[] args) {
        int score = 85;
        
        if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 80) {
            System.out.println("良好");
        } else if (score >= 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
    }
}

7.2 switch语句

public class SwitchDemo {
    public static void main(String[] args) {
        int day = 3;
        
        switch (day) {
            case 1:
                System.out.println("周一");
                break;
            case 2:
                System.out.println("周二");
                break;
            case 3:
                System.out.println("周三");
                break;
            default:
                System.out.println("其他天");
        }
    }
}

7.3 循环语句

public class Loops {
    public static void main(String[] args) {
        // for循环
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
        
        // while循环
        int j = 0;
        while (j < 5) {
            System.out.println(j);
            j++;
        }
        
        // do-while循环
        int k = 0;
        do {
            System.out.println(k);
            k++;
        } while (k < 5);
        
        // 嵌套循环
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.println(i + "-" + j);
            }
        }
    }
}

八、数组详解

8.1 数组声明与初始化

public class ArrayDemo {
    public static void main(String[] args) {
        // 静态初始化
        int[] arr1 = {1, 2, 3, 4, 5};
        
        // 动态初始化
        int[] arr2 = new int[5];
        arr2[0] = 1;
        arr2[1] = 2;
        arr2[2] = 3;
        
        // 多维数组
        int[][] matrix = new int[2][3];
        matrix[0][0] = 1;
        matrix[0][1] = 2;
        matrix[0][2] = 3;
        matrix[1][0] = 4;
        matrix[1][1] = 5;
        matrix[1][2] = 6;
        
        System.out.println(matrix.length); // 2
        System.out.println(matrix[0].length); // 3
    }
}

8.2 数组常用操作

public class ArrayOperations {
    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 1, 5, 9, 2, 6};
        
        // 遍历数组
        System.out.println("遍历数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        
        // 查找最大值
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("最大值: " + max);
        
        // 查找最小值
        int min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("最小值: " + min);
        
        // 数组排序
        int[] sorted = arr.clone();
        Arrays.sort(sorted);
        System.out.println("排序后: " + Arrays.toString(sorted));
    }
}

九、常见面试题

9.1 基础面试题

  1. 基本数据类型有哪些?

    • 8种:byte, short, int, long, float, double, char, boolean
  2. String为什么是不可变的?

    • 线程安全
    • 缓存hashcode
    • 提高性能
    • 便于字符串常量池管理
  3. ==和equals的区别?

    • ==比较内存地址
    • equals比较内容(可重写)
  4. int和Integer的区别?

    • int是基本类型,Integer是包装类
    • Integer可以null,int不能
    • Integer有自动装箱拆箱
  5. 包装类的缓存范围?

    • Integer: -128到127
    • Long: -128到127
    • Short: -128到127
    • Byte: -128到127

9.2 进阶面试题

  1. String s = new String("abc") 创建几个对象?

    • 2个:字面量"abc"在常量池,new对象在堆
  2. String s = "a" + "b" + "c"创建几个对象?

    • 1个:编译时优化,只创建一个对象
  3. String.intern()的作用?

    • 如果字符串常量池中没有,则添加
    • 返回常量池中的引用
  4. 如何重写equals方法?

    • 检查对象引用
    • 检查类型
    • 检查字段值
    • 同时重写hashCode

十、总结

Java基础是面试的基石,掌握好这些知识点:

  1. 基本数据类型:了解类型范围、默认值
  2. String原理:理解不可变性、常量池机制
  3. equals与==:区分比较方式
  4. 包装类:理解自动装箱拆箱、缓存机制
  5. 类型转换:掌握各种转换方法
  6. 数组操作:熟练使用数组常用方法

掌握这些知识点,能为后续学习框架、并发、JVM等高级内容打下坚实基础!

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐