Java基础知识总结(超详细整理)
- 2025-11-04 01:01:59
以下是Java基础知识的详细总结,包含核心概念和代码示例,涵盖语法、面向对象、常用类等关键内容:
一、Java基础语法1. 基本数据类型Java有8种基本数据类型,分为数值型、字符型、布尔型:
代码语言:java复制public class BasicTypes {
public static void main(String[] args) {
// 整数型
byte b = 127; // 1字节,范围:-128~127
short s = 32767; // 2字节
int i = 2147483647; // 4字节(默认整数类型)
long l = 9223372036854775807L; // 8字节,需加L后缀
// 浮点型
float f = 3.14F; // 4字节,需加F后缀
double d = 3.1415926; // 8字节(默认浮点类型)
// 字符型
char c = 'A'; // 2字节,Unicode字符
char chinese = '中'; // 支持中文
// 布尔型
boolean flag = true; // 1位,仅true/false
}
}2. 运算符包括算术、赋值、比较、逻辑、位运算等:
代码语言:java复制public class Operators {
public static void main(String[] args) {
// 算术运算符
int a = 10, b = 3;
System.out.println(a + b); // 13
System.out.println(a / b); // 3(整数除法)
System.out.println(a % b); // 1(取余)
System.out.println(++a); // 11(先自增后使用)
// 逻辑运算符
boolean x = true, y = false;
System.out.println(x && y); // false(短路与)
System.out.println(x || y); // true(短路或)
// 位运算符(操作二进制位)
int c = 6; // 二进制:110
int d = 3; // 二进制:011
System.out.println(c & d); // 2(010,按位与)
System.out.println(c | d); // 7(111,按位或)
System.out.println(c ^ d); // 5(101,按位异或)
}
}3. 流程控制条件语句:if-else、switchpublic class Condition {
public static void main(String[] args) {
int score = 85;
// if-else
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
// switch(JDK12+支持表达式)
String day = "MON";
switch (day) {
case "MON", "TUE", "WED", "THU", "FRI" -> System.out.println("工作日");
case "SAT", "SUN" -> System.out.println("周末");
default -> System.out.println("无效日期");
}
}
}循环语句:for、while、do-whilepublic class Loops {
public static void main(String[] args) {
// for循环
for (int i = 0; i < 5; i++) {
System.out.print(i + " "); // 0 1 2 3 4
}
// 增强for循环(遍历数组/集合)
int[] arr = {1, 2, 3};
for (int num : arr) {
System.out.print(num + " "); // 1 2 3
}
// while循环
int count = 0;
while (count < 3) {
System.out.println("count: " + count);
count++;
}
}
}二、面向对象编程(OOP)1. 类与对象类是模板,对象是类的实例:
代码语言:java复制// 定义类
class Person {
// 属性(成员变量)
String name;
int age;
// 方法(行为)
void sayHello() {
System.out.println("Hello, 我叫" + name + ", 今年" + age + "岁");
}
}
// 使用类创建对象
public class ClassObject {
public static void main(String[] args) {
Person p = new Person(); // 创建对象
p.name = "张三"; // 赋值属性
p.age = 20;
p.sayHello(); // 调用方法:Hello, 我叫张三, 今年20岁
}
}2. 构造方法用于初始化对象,与类名相同且无返回值:
代码语言:java复制class Student {
String name;
int id;
// 无参构造
Student() {}
// 有参构造
Student(String n, int i) {
name = n;
id = i;
}
void show() {
System.out.println("姓名:" + name + ",学号:" + id);
}
}
public class Constructor {
public static void main(String[] args) {
Student s1 = new Student(); // 调用无参构造
s1.name = "李四";
s1.id = 1001;
Student s2 = new Student("王五", 1002); // 调用有参构造
s2.show(); // 姓名:王五,学号:1002
}
}3. 封装通过private修饰属性,提供get/set方法访问:
代码语言:java复制class Car {
private String brand; // 私有属性,外部无法直接访问
private int price;
// get方法:获取属性值
public String getBrand() {
return brand;
}
// set方法:设置属性值(可加验证)
public void setBrand(String b) {
if (b != null) {
brand = b;
}
}
public int getPrice() {
return price;
}
public void setPrice(int p) {
if (p > 0) { // 价格必须为正数
price = p;
}
}
}
public class Encapsulation {
public static void main(String[] args) {
Car car = new Car();
car.setBrand("特斯拉");
car.setPrice(-100000); // 无效值,不生效
System.out.println(car.getBrand() + "价格:" + car.getPrice()); // 特斯拉价格:0
}
}4. 继承使用extends关键字,子类继承父类的属性和方法:
代码语言:java复制// 父类
class Animal {
String name;
void eat() {
System.out.println(name + "在吃东西");
}
}
// 子类继承父类
class Dog extends Animal {
// 重写父类方法
@Override
void eat() {
System.out.println(name + "在吃骨头");
}
// 子类特有方法
void bark() {
System.out.println(name + "在汪汪叫");
}
}
public class Inheritance {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "旺财";
dog.eat(); // 旺财在吃骨头(调用重写的方法)
dog.bark(); // 旺财在汪汪叫(子类特有)
}
}5. 多态同一行为的不同表现形式(父类引用指向子类对象):
代码语言:java复制class Cat extends Animal {
@Override
void eat() {
System.out.println(name + "在吃鱼");
}
}
public class Polymorphism {
public static void main(String[] args) {
Animal animal1 = new Dog(); // 父类引用指向Dog对象
Animal animal2 = new Cat(); // 父类引用指向Cat对象
animal1.name = "小白";
animal2.name = "小花";
animal1.eat(); // 小白在吃骨头(调用Dog的eat)
animal2.eat(); // 小花在吃鱼(调用Cat的eat)
}
}6. 抽象类与接口抽象类:用abstract修饰,可包含抽象方法(无实现)和普通方法abstract class Shape {
abstract double getArea(); // 抽象方法,子类必须实现
}
class Circle extends Shape {
double radius;
Circle(double r) {
radius = r;
}
@Override
double getArea() {
return Math.PI * radius * radius; // 圆面积公式
}
}接口:用interface修饰,全是抽象方法(JDK8+可有默认方法)interface Flyable {
void fly(); // 抽象方法
default void land() { // 默认方法
System.out.println("降落");
}
}
class Bird implements Flyable {
@Override
public void fly() {
System.out.println("鸟在飞");
}
}三、常用类1. String类不可变字符串,常用方法:
代码语言:java复制public class StringDemo {
public static void main(String[] args) {
String s = "Hello Java";
System.out.println(s.length()); // 长度:9
System.out.println(s.charAt(1)); // 索引1的字符:e
System.out.println(s.substring(6)); // 从索引6截取:Java
System.out.println(s.contains("Java")); // 是否包含:true
System.out.println(s.toUpperCase()); // 转大写:HELLO JAVA
}
}2. 包装类基本类型的包装对象,提供类型转换等方法:
代码语言:java复制public class WrapperClass {
public static void main(String[] args) {
// 装箱:基本类型→包装类
Integer i1 = Integer.valueOf(100);
Integer i2 = 200; // 自动装箱
// 拆箱:包装类→基本类型
int num = i1.intValue();
int sum = i1 + i2; // 自动拆箱
// 字符串转基本类型
int a = Integer.parseInt("123");
double b = Double.parseDouble("3.14");
}
}3. 集合框架常用集合:ArrayList(动态数组)、HashMap(键值对)
代码语言:java复制import java.util.ArrayList;
import java.util.HashMap;
public class Collections {
public static void main(String[] args) {
// ArrayList
ArrayList
list.add("苹果");
list.add("香蕉");
System.out.println(list.get(0)); // 获取第一个元素:苹果
for (String fruit : list) {
System.out.println(fruit);
}
// HashMap
HashMap
map.put("语文", 90);
map.put("数学", 85);
System.out.println(map.get("语文")); // 获取值:90
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
}4. 异常处理使用try-catch-finally捕获异常:
代码语言:java复制public class ExceptionHandling {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
try {
System.out.println(arr[3]); // 数组越界,抛出异常
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("异常:" + e.getMessage()); // 捕获异常
} finally {
System.out.println("无论是否异常,都会执行"); // 释放资源
}
}
}四、其他重要知识点static关键字:修饰静态成员(属于类,而非对象)class Tool {
static String version = "1.0"; // 静态变量
static void printVersion() { // 静态方法
System.out.println("版本:" + version);
}
}
public class StaticDemo {
public static void main(String[] args) {
// 直接通过类名访问,无需创建对象
Tool.printVersion(); // 版本:1.0
System.out.println(Tool.version);
}
}this与super:this指代当前对象,super指代父类对象class Parent {
String name = "父类";
}
class Child extends Parent {
String name = "子类";
void show() {
System.out.println(this.name); // 子类
System.out.println(super.name); // 父类
}
}以上内容涵盖了Java基础的核心知识点,通过代码示例可直观理解语法规则和面向对象特性。实际开发中需结合具体场景灵活运用,进一步学习集合、IO、多线程等进阶内容。
