Java 入门可以按照核心知识点分类学习,以下从「基础语法」「面向对象」「常用工具类」「异常处理」「集合框架」5 大模块进行介绍,并附带代码示例,帮助快速入门。
一、基础语法
Java 基础语法是编程的基石,包括变量、数据类型、运算符、流程控制等。
1. 变量与数据类型
Java 是强类型语言,变量必须先声明后使用,有 8 种基本数据类型和引用类型。
public class BasicTypesDemo {
public static void main(String[] args) {
// 基本数据类型
int age = 20; // 整数(4字节)
double height = 1.75; // 浮点数(8字节)
char gender = '男'; // 字符(2字节,支持中文)
boolean isStudent = true; // 布尔值(true/false)
// 引用类型(字符串)
String name = "张三";
// 输出变量
System.out.println("姓名:" + name + ",年龄:" + age);
}
}
2. 运算符
包括算术、赋值、比较、逻辑等运算符,用于数据计算和逻辑判断。
public class OperatorsDemo {
public static void main(String[] args) {
int a = 10, b = 3;
System.out.println("加法:" + (a + b)); // 13
System.out.println("取余:" + (a % b)); // 1
System.out.println("大于?" + (a > b)); // true
// 逻辑运算符(短路与:两边都为true才返回true)
boolean flag = (a > 5) && (b < 5);
System.out.println("逻辑结果:" + flag); // true
}
}
3. 流程控制
通过条件判断和循环控制程序执行顺序。
public class FlowControlDemo {
public static void main(String[] args) {
// 1. if-else 条件判断
int score = 85;
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
// 2. for 循环(输出1-5)
for (int i = 1; i <= 5; i++) {
System.out.print(i + " ");
}
// 3. while 循环(输出1-3)
int num = 1;
while (num <= 3) {
System.out.println("\nnum:" + num);
num++;
}
}
}
二、面向对象编程(OOP)
Java 是面向对象语言,核心思想是「封装、继承、多态」。
1. 类与对象
类是模板,对象是类的实例(具体存在)。
// 定义类(模板)
class Person {
// 属性(特征)
String name;
int age;
// 方法(行为)
void introduce() {
System.out.println("我叫" + name + ",今年" + age + "岁");
}
}
// 使用类创建对象
public class ClassObjectDemo {
public static void main(String[] args) {
// 创建对象(实例化)
Person person = new Person();
// 给属性赋值
person.name = "李四";
person.age = 25;
// 调用方法
person.introduce(); // 输出:我叫李四,今年25岁
}
}
2. 封装
通过 private 隐藏属性,提供 get/set 方法控制访问,保证数据安全。
class Student {
// 私有属性(外部不能直接访问)
private String id;
private int score;
// get方法:获取属性值
public String getId() {
return id;
}
// set方法:设置属性值(可加验证)
public void setId(String id) {
this.id = id; // this代表当前对象
}
public int getScore() {
return score;
}
public void setScore(int score) {
// 分数必须在0-100之间
if (score >= 0 && score <= 100) {
this.score = score;
} else {
System.out.println("分数无效");
}
}
}
public class EncapsulationDemo {
public static void main(String[] args) {
Student stu = new Student();
stu.setId("2023001");
stu.setScore(150); // 无效分数,输出提示
stu.setScore(90);
System.out.println("学号:" + stu.getId() + ",分数:" + stu.getScore());
}
}
3. 继承
子类通过 extends 继承父类,复用代码并扩展功能。
// 父类(基类)
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 InheritanceDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "旺财";
dog.eat(); // 调用重写的方法:旺财在吃骨头
dog.bark(); // 调用子类特有方法:旺财在汪汪叫
}
}
4. 多态
同一行为的不同表现形式(父类引用指向子类对象)。
class Cat extends Animal {
@Override
void eat() {
System.out.println(name + "在吃鱼");
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
// 父类引用指向Dog对象
Animal animal1 = new Dog();
// 父类引用指向Cat对象
Animal animal2 = new Cat();
animal1.name = "小白";
animal2.name = "小花";
animal1.eat(); // 小白在吃骨头(执行Dog的eat)
animal2.eat(); // 小花在吃鱼(执行Cat的eat)
}
}
三、常用工具类
Java 提供了丰富的工具类,简化开发,如 String、Math、Date 等。
1. String 类(字符串)
处理文本数据,常用方法:
public class StringDemo {
public static void main(String[] args) {
String s = "Hello Java";
System.out.println("长度:" + s.length()); // 9
System.out.println("是否包含Java:" + s.contains("Java")); // true
System.out.println("截取子串:" + s.substring(6)); // Java
System.out.println("转大写:" + s.toUpperCase()); // HELLO JAVA
}
}
2. Math 类(数学运算)
提供常用数学方法:
public class MathDemo {
public static void main(String[] args) {
System.out.println("绝对值:" + Math.abs(-5)); // 5
System.out.println("最大值:" + Math.max(3, 7)); // 7
System.out.println("随机数(0-1):" + Math.random()); // 如0.345
System.out.println("四舍五入:" + Math.round(3.7)); // 4
}
}
四、异常处理
程序运行时可能出现错误(如数组越界),通过异常处理避免程序崩溃。
public class ExceptionDemo {
public static void main(String[] args) {
int[] arr = {
1, 2, 3};
try {
// 可能出错的代码(数组索引最大为2,这里访问3会报错)
System.out.println(arr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
// 捕获异常并处理
System.out.println("错误:数组索引越界");
} finally {
// 无论是否出错,都会执行(常用于释放资源)
System.out.println("程序执行结束");
}
}
}
五、集合框架
用于存储多个数据,比数组更灵活(动态扩容、支持多种操作)。
1. ArrayList(动态数组)
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String[] args) {
// 创建集合(存储字符串)
ArrayList<String> list = new ArrayList<>();
// 添加元素
list.add("苹果");
list.add("香蕉");
list.add("橙子");
// 遍历集合(增强for循环)
for (String fruit : list) {
System.out.println(fruit);
}
// 获取指定位置元素
System.out.println("第2个元素:" + list.get(1)); // 香蕉
// 删除元素
list.remove(0);
System.out.println("删除后大小:" + list.size()); // 2
}
}
2. HashMap(键值对集合)
import java.util.HashMap;
public class HashMapDemo {
public static void main(String[] args) {
// 创建键值对集合(键:String,值:Integer)
HashMap<String, Integer> map = new HashMap<>();
// 添加键值对
map.put("语文", 90);
map.put("数学", 85);
map.put("英语", 95);
// 获取值(通过键)
System.out.println("数学成绩:" + map.get("数学")); // 85
// 遍历所有键
for (String subject : map.keySet()) {
System.out.println(subject + ":" + map.get(subject));
}
}
}
总结
Java 入门核心分类:
- 基础语法:变量、运算符、流程控制(构建程序骨架)。
- 面向对象:类与对象、封装、继承、多态(Java 核心思想)。
- 工具类:
String、Math等(简化常用操作)。 - 异常处理:
try-catch(保证程序健壮性)。 - 集合框架:
ArrayList、HashMap等(高效存储数据)。
通过以上分类逐步学习,结合代码练习,可快速掌握 Java 基础。后续可深入学习多线程、IO 流、网络编程等进阶内容。