2026版 Java零基础小白学习知识点【基础版详解】

✅ 逐点拆解+白话解释+代码示例,零基础能直接照着敲
✅ 2026技术适配:基于Java 21 LTS(企业主流)+ Spring Boot 3.x,剔除所有过时知识点
✅ 条理清晰:从“是什么→怎么用→实战代码→避坑点”层层拆解,每个知识点都落地到代码
✅ 核心目标:小白不仅“懂概念”,更能“写得出、跑得起”,真正入门Java开发


一、前置准备:先搞定环境和核心认知

1. 2026年必装工具(一步到位,不用折腾)

工具作用安装要点(白话版)
JDK 21 LTSJava开发的核心环境官网下载对应系统版本(Windows选exe,Mac选dmg),安装时一路下一步,记得配置环境变量(IDEA会自动识别,新手可忽略)
IntelliJ IDEA 2025.3(社区版)写Java代码的编辑器官网下载免费社区版,安装后直接用,不用破解,默认设置即可
MySQL 8.4数据库(存数据用)安装时设置root密码(记牢!),推荐用Navicat/MySQL Workbench可视化操作(新手友好)
Postman 10+调试接口用官网下载免费版,注册账号即可,不用付费功能

2. 核心概念(不用背,理解就行)

  • JDK:你写代码需要的“工具箱”(包含编译器、运行工具等),装了JDK就不用单独装JRE/JVM;
  • IDEA:帮你写代码的“智能记事本”,能自动补全、查错、运行代码;
  • 后端接口:给前端返回数据的“通道”,比如前端调/student/1,后端返回ID=1的学生信息;
  • JSON:前后端数据交互的“通用语言”,格式像{"name":"小明","age":18},所有人都能看懂。

二、第一部分:Java基础语法(敲完这些,才算入门)

1. 第一个Java程序:Hello World(跑起来就成功一半)

代码示例(直接复制到IDEA):
// 注释:这是Java程序的入口类,类名要和文件名一致
public class HelloWorld {
    // main方法:程序的入口,固定写法
    public static void main(String[] args) {
        // 输出语句:向控制台打印内容
        System.out.println("你好,Java 21!");
    }
}
关键要点:
  • 类名HelloWorld必须和文件名一致,且首字母大写;
  • main方法是程序的“启动按钮”,固定格式public static void main(String[] args)
  • System.out.println()是“打印到控制台”,括号里写要输出的内容;
  • 每行代码结束必须加;,这是Java的“规矩”。
运行结果:

控制台会输出:你好,Java 21!

2. 变量与数据类型(存储数据的“盒子”)

核心数据类型(2026年企业常用):
类型用途示例
int存整数(年龄、分数、数量)int age = 18;
double存小数(工资、价格、身高)double salary = 5000.5;
String存文字(姓名、地址)String name = "小明";
boolean存真假(是否成年、是否登录)boolean isAdult = true;
代码示例:
public class VariableDemo {
    public static void main(String[] args) {
        // 定义变量并赋值
        String name = "小明";
        int age = 18;
        double score = 92.5;
        boolean isPass = score >= 60;
        
        // 打印变量(拼接用+号)
        System.out.println("姓名:" + name);
        System.out.println("年龄:" + age);
        System.out.println("成绩:" + score);
        System.out.println("是否及格:" + isPass);
    }
}
避坑点:
  • 变量名不能以数字开头,不能有空格,比如1ageuser name都是错的;
  • String是“字符串”,首字母大写,不是string
  • 布尔类型只有true/false,不能写1/0

3. 条件判断:if/else(让程序“做选择”)

代码示例(成绩评级):
public class IfDemo {
    public static void main(String[] args) {
        int score = 85;
        
        if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 60) {
            System.out.println("及格");
        } else {
            System.out.println("不及格");
        }
    }
}
关键要点:
  • 条件写在()里,比如score >= 90
  • 满足条件的代码写在{}里,即使只有一行,也建议加{}(规范);
  • 可以多层嵌套,比如在“优秀”里再判断是否满分。

4. 循环:for/while(让程序“重复做事”)

(1)for循环(知道循环次数时用)
public class ForDemo {
    public static void main(String[] args) {
        // 打印1-10的数字
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
        
        // 计算1-100的和
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i; // 等价于sum = sum + i
        }
        System.out.println("1-100的和:" + sum); // 输出5050
    }
}
(2)while循环(不知道循环次数时用)
public class WhileDemo {
    public static void main(String[] args) {
        // 打印1-5的数字
        int i = 1;
        while (i <= 5) {
            System.out.println(i);
            i++; // 必须写,否则死循环
        }
    }
}
避坑点:
  • for循环的i++不能忘,while循环的变量自增不能忘,否则会“死循环”(程序卡死);
  • 循环条件要写对,比如i <= 100不是i < 100(否则少算100)。

5. 方法(函数):封装重复代码

代码示例(计算平均分):
public class MethodDemo {
    public static void main(String[] args) {
        // 定义成绩数组
        int[] scores = {80, 90, 85, 95};
        // 调用方法计算平均分
        double avg = calculateAvg(scores);
        System.out.println("平均分:" + avg); // 输出87.5
    }
    
    // 定义计算平均分的方法
    public static double calculateAvg(int[] arr) {
        int sum = 0;
        for (int num : arr) { // 增强for循环,遍历数组
            sum += num;
        }
        return (double) sum / arr.length; // 转成小数避免整除
    }
}
关键要点:
  • 方法定义格式:public static 返回值类型 方法名(参数类型 参数名) { 代码 }
  • 返回值类型要和return的内容一致,比如返回double就不能return int;
  • 无返回值时用void,比如public static void printHello() { ... }

三、第二部分:面向对象(Java的灵魂)

1. 类和对象:把现实事物抽象成代码

代码示例(学生类):
// 定义学生类(模板)
public class Student {
    // 属性(特征):私有,封装
    private String name;
    private int age;
    
    // 构造方法:创建对象时初始化属性
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // 方法(行为):打印学生信息
    public void printInfo() {
        System.out.println("姓名:" + name + ",年龄:" + age);
    }
    
    // get/set方法:访问私有属性(封装要求)
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}

// 测试类
public class StudentTest {
    public static void main(String[] args) {
        // 创建对象(实例)
        Student stu1 = new Student("小明", 18);
        Student stu2 = new Student("小红", 17);
        
        // 调用方法
        stu1.printInfo(); // 输出:姓名:小明,年龄:18
        stu2.printInfo(); // 输出:姓名:小红,年龄:17
        
        // 修改属性(通过set方法)
        stu1.setAge(19);
        stu1.printInfo(); // 输出:姓名:小明,年龄:19
    }
}
关键要点:
  • 类是“模板”,比如“学生模板”;对象是“具体的人”,比如“小明”;
  • 属性私有化(private),通过get/set方法访问,这是“封装”;
  • 构造方法名和类名一致,没有返回值(连void都不用写)。

2. 继承:子类复用父类代码

代码示例:
// 父类:学生
public class Student {
    protected String name; // 受保护,子类能访问
    protected int age;
    
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void study() {
        System.out.println(name + "在学习");
    }
}

// 子类:大学生(继承学生)
public class CollegeStudent extends Student {
    private String major; // 新增属性:专业
    
    // 子类构造方法
    public CollegeStudent(String name, int age, String major) {
        super(name, age); // 调用父类构造方法
        this.major = major;
    }
    
    // 重写父类方法
    @Override
    public void study() {
        System.out.println(name + "在学习" + major + "专业");
    }
    
    // 新增方法
    public void attendPractice() {
        System.out.println(name + "参加实习");
    }
}

// 测试类
public class InheritTest {
    public static void main(String[] args) {
        CollegeStudent cs = new CollegeStudent("小李", 20, "计算机");
        cs.study(); // 输出:小李在学习计算机专业
        cs.attendPractice(); // 输出:小李参加实习
    }
}
关键要点:
  • 继承用extends,Java只能单继承(一个子类只能有一个父类);
  • super()必须写在子类构造方法的第一行,调用父类构造;
  • 重写父类方法时加@Override注解,规范且易读。

3. 接口:定义“规范”

代码示例(支付接口):
// 定义支付接口(规范)
public interface Pay {
    // 接口中的方法默认是public abstract
    void pay(double money);
}

// 微信支付实现接口
public class WeChatPay implements Pay {
    @Override
    public void pay(double money) {
        System.out.println("微信支付" + money + "元");
    }
}

// 支付宝支付实现接口
public class AliPay implements Pay {
    @Override
    public void pay(double money) {
        System.out.println("支付宝支付" + money + "元");
    }
}

// 测试类
public class PayTest {
    public static void main(String[] args) {
        Pay pay1 = new WeChatPay();
        pay1.pay(100); // 输出:微信支付100.0元
        
        Pay pay2 = new AliPay();
        pay2.pay(50); // 输出:支付宝支付50.0元
    }
}
关键要点:
  • 接口用interface定义,实现用implements
  • 接口中只能定义方法声明(2026年也可写默认方法default),不能写方法体;
  • 一个类可以实现多个接口,弥补Java单继承的不足。

四、第三部分:核心工具类(提升开发效率)

1. 字符串处理(String)

代码示例:
public class StringDemo {
    public static void main(String[] args) {
        String str = "  Hello Java 2026  ";
        
        // 去除首尾空格
        String trimStr = str.trim();
        System.out.println(trimStr); // 输出:Hello Java 2026
        
        // 转大写
        String upperStr = trimStr.toUpperCase();
        System.out.println(upperStr); // 输出:HELLO JAVA 2026
        
        // 截取字符串(从索引6开始)
        String subStr = trimStr.substring(6);
        System.out.println(subStr); // 输出:Java 2026
        
        // 替换字符串
        String replaceStr = trimStr.replace("Java", "Spring Boot");
        System.out.println(replaceStr); // 输出:Hello Spring Boot 2026
    }
}

2. 集合:List和Map(比数组好用10倍)

(1)List:存储有序的集合(可重复)
import java.util.ArrayList;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        // 创建List集合
        List<String> list = new ArrayList<>();
        
        // 添加元素
        list.add("小明");
        list.add("小红");
        list.add("小李");
        
        // 遍历集合
        for (String name : list) {
            System.out.println(name);
        }
        
        // 获取指定位置元素
        String first = list.get(0);
        System.out.println("第一个元素:" + first); // 输出:小明
        
        // 删除元素
        list.remove(1); // 删除小红
        System.out.println("删除后:" + list); // 输出:[小明, 小李]
    }
}
(2)Map:存储键值对(键唯一,值可重复)
import java.util.HashMap;
import java.util.Map;

public class MapDemo {
    public static void main(String[] args) {
        // 创建Map集合
        Map<String, Integer> scoreMap = new HashMap<>();
        
        // 添加键值对
        scoreMap.put("小明", 90);
        scoreMap.put("小红", 85);
        scoreMap.put("小李", 95);
        
        // 获取值
        int xiaoMingScore = scoreMap.get("小明");
        System.out.println("小明成绩:" + xiaoMingScore); // 输出:90
        
        // 遍历Map
        for (Map.Entry<String, Integer> entry : scoreMap.entrySet()) {
            System.out.println("姓名:" + entry.getKey() + ",成绩:" + entry.getValue());
        }
    }
}

3. 异常处理:try-catch(避免程序崩溃)

代码示例:
public class ExceptionDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 0;
        
        try {
            // 可能出错的代码
            int result = a / b;
            System.out.println(result);
        } catch (ArithmeticException e) {
            // 捕获除数为0的异常
            System.out.println("错误:除数不能为0");
            // 打印异常详情(调试用)
            e.printStackTrace();
        } finally {
            // 无论是否出错,都会执行
            System.out.println("程序执行完毕");
        }
    }
}
关键要点:
  • try里放可能出错的代码,catch捕获指定异常,finally放必须执行的代码(如关闭文件);
  • 常见异常:NullPointerException(空指针)、ArrayIndexOutOfBoundsException(数组越界)。

4. 日期处理:LocalDateTime(2026年主流)

代码示例:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateDemo {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间:" + now); // 输出:2026-01-07T15:30:20.123
        
        // 格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatNow = now.format(formatter);
        System.out.println("格式化后:" + formatNow); // 输出:2026-01-07 15:30:20
        
        // 计算3天后的时间
        LocalDateTime after3Days = now.plusDays(3);
        System.out.println("3天后:" + after3Days.format(formatter));
    }
}
避坑点:
  • 不要再学Date/SimpleDateFormat(老旧、线程不安全),2026年企业只认LocalDateTime
  • 格式化模式要写对,比如yyyy是4位年,MM是2位月,dd是2位日。

五、第四部分:数据库交互(后端核心)

1. MySQL基础:增删改查(CRUD)

核心SQL语句:
-- 1. 创建学生表
CREATE TABLE student (
    id INT PRIMARY KEY AUTO_INCREMENT, -- 主键自增
    name VARCHAR(20) NOT NULL, -- 姓名,非空
    age INT,
    score DOUBLE
);

-- 2. 新增数据
INSERT INTO student (name, age, score) VALUES ('小明', 18, 90);

-- 3. 查询数据
SELECT * FROM student; -- 查询所有
SELECT * FROM student WHERE id = 1; -- 查询ID=1的学生

-- 4. 修改数据
UPDATE student SET score = 95 WHERE id = 1;

-- 5. 删除数据
DELETE FROM student WHERE id = 1;

2. MyBatis操作数据库(2026年主流)

(1)核心配置(application.yml):
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: 你的MySQL密码
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper/*.xml -- mapper文件位置
  type-aliases-package: com.example.demo.entity -- 实体类包名
(2)实体类(Student.java):
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private Double score;
    
    // 无参构造、有参构造、get/set方法、toString方法
}
(3)Mapper接口(StudentMapper.java):
import org.apache.ibatis.annotations.Select;
import java.util.List;

public interface StudentMapper {
    // 查询所有学生
    @Select("SELECT * FROM student")
    List<Student> findAll();
    
    // 根据ID查询
    @Select("SELECT * FROM student WHERE id = #{id}")
    Student findById(Integer id);
}
(4)测试代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.Test;
import java.util.List;

@SpringBootTest
public class MyBatisTest {
    @Autowired
    private StudentMapper studentMapper;
    
    @Test
    public void testFindAll() {
        List<Student> list = studentMapper.findAll();
        for (Student stu : list) {
            System.out.println(stu);
        }
    }
}

六、第五部分:Spring Boot基础(实战入门)

1. 快速创建Spring Boot项目(IDEA)

  1. 打开IDEA → 新建项目 → 选择Spring Initializr;
  2. 填写项目名(如demo)、包名 → 选择Java 21;
  3. 勾选依赖:Spring Web、MyBatis Framework、MySQL Driver;
  4. 点击创建,IDEA会自动下载依赖。

2. 写第一个接口(Controller)

代码示例:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController // 标识这是控制器,返回JSON
public class StudentController {
    
    // 简单接口:返回字符串
    @GetMapping("/hello")
    public String hello() {
        return "你好,Spring Boot 3.x!";
    }
    
    // 带参数接口:根据ID查询学生(模拟)
    @GetMapping("/student/{id}")
    public Student getStudent(@PathVariable Integer id) {
        // 模拟数据,实际会调用Service查数据库
        Student stu = new Student();
        stu.setId(id);
        stu.setName("小明");
        stu.setAge(18);
        stu.setScore(90.0);
        return stu;
    }
}
运行项目:
  • 点击IDEA的“启动”按钮,项目启动后,浏览器访问:
    • http://localhost:8080/hello → 看到“你好,Spring Boot 3.x!”;
    • http://localhost:8080/student/1 → 看到JSON数据:{"id":1,"name":"小明","age":18,"score":90.0}

3. 三层架构:Controller → Service → Mapper

(1)Service层(StudentService.java):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service // 标识服务层
public class StudentService {
    @Autowired
    private StudentMapper studentMapper;
    
    // 查询所有学生
    public List<Student> findAll() {
        return studentMapper.findAll();
    }
}
(2)Controller调用Service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    
    @GetMapping("/students")
    public List<Student> getAllStudents() {
        return studentService.findAll();
    }
}
运行结果:

访问http://localhost:8080/students,会返回数据库中所有学生的JSON列表。


七、核心避坑清单(小白必看)

  1. ❌ 类名小写、方法名大写 → ✅ 类名首字母大写(Pascal命名),方法名首字母小写(驼峰命名);
  2. ❌ 忘记加注解(如@RestController、@Service) → ✅ Spring Boot靠注解识别组件,不加会报错;
  3. ❌ MySQL连接地址写错 → ✅ 检查端口(默认3306)、库名、时区(serverTimezone=Asia/Shanghai);
  4. ❌ 接口返回null → ✅ 检查数据库是否有数据、Mapper是否正确、Service是否注入;
  5. ❌ 依赖下载失败 → ✅ 换国内镜像(阿里云),或手动下载依赖。

八、总结

  1. 基础语法是地基:变量、循环、方法必须练熟,能独立写控制台程序;
  2. 面向对象是核心:理解类、对象、继承、接口,才能看懂企业级代码;
  3. 数据库+Spring Boot是实战关键:2026年Java后端的核心是“操作数据库+提供接口”,这两部分要结合练;
  4. 动手实操是唯一捷径:每个知识点都要敲代码、跑起来,哪怕是复制修改,也比只看视频强。

这份详解覆盖了Java零基础入门的所有核心点,跟着代码示例敲一遍,跑通所有案例,你就能真正入门Java开发,为后续进阶学微服务、高并发打下坚实基础。


全文结束,祝你Java学习一路畅通!🚀

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT=>小脑虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值