01. Java 基础语法
Java 是一种面向对象的编程语言,以"一次编写,到处运行"著称。
Java 的特点:
- 强类型静态语言
- 面向对象
- 自动垃圾回收
- 跨平台(JVM)
- 丰富的标准库
java
public class HelloWorld {
public static void main(String[] args) {
// 变量声明
int age = 25;
double price = 9.99;
String name = "张三";
boolean isActive = true;
// 字符串操作
String greeting = "你好, " + name + "!";
System.out.println(greeting);
System.out.println("长度: " + name.length());
System.out.println("大写: " + name.toUpperCase());
// 数组
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.print(num + " ");
}
// 条件语句
if (age >= 18) {
System.out.println("成年人");
} else {
System.out.println("未成年人");
}
// 循环
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
}
}02. 面向对象编程
Java 是纯面向对象语言,所有代码都必须在类中。
类和对象:
- 类是对象的模板
- 对象是类的实例
- 字段(属性)描述对象的状态
- 方法描述对象的行为
三大特性:
- 封装:隐藏内部实现
- 继承:子类继承父类
- 多态:同一接口不同实现
java
// 定义类
class Animal {
private String name;
private int age;
// 构造方法
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
// Getter 和 Setter
public String getName() { return name; }
public void setName(String name) { this.name = name; }
// 方法
public void speak() {
System.out.println(name + " 发出声音");
}
@Override
public String toString() {
return "Animal{name='" + name + "', age=" + age + "}";
}
}
// 继承
class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
@Override
public void speak() {
System.out.println(getName() + ": 汪汪!");
}
}
// 使用
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("旺财", 3, "金毛");
dog.speak(); // 旺财: 汪汪!
System.out.println(dog);
}
}03. 异常处理
Java 使用 try-catch-finally 机制处理异常。
异常分类:
- Checked Exception:编译时检查(IOException、SQLException)
- Unchecked Exception:运行时异常(NullPointerException、ArrayIndexOutOfBoundsException)
- Error:系统错误(OutOfMemoryError)
java
// 基本异常处理
public class ExceptionDemo {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println("结果: " + result);
} catch (ArithmeticException e) {
System.out.println("算术错误: " + e.getMessage());
} finally {
System.out.println("始终执行");
}
// 多重 catch
try {
String str = null;
str.length(); // NullPointerException
} catch (NullPointerException e) {
System.out.println("空指针异常");
} catch (Exception e) {
System.out.println("其他异常");
}
// 自定义异常
try {
validateAge(-5);
} catch (InvalidAgeException e) {
System.out.println(e.getMessage());
}
}
static int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("除数不能为零");
}
return a / b;
}
static void validateAge(int age) throws InvalidAgeException {
if (age < 0 || age > 150) {
throw new InvalidAgeException("年龄无效: " + age);
}
}
}
// 自定义异常
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}04. 集合框架
Java 集合框架提供了数据结构的统一接口。
主要接口:
- Collection:存储一组对象
- List:有序可重复(ArrayList、LinkedList)
- Set:无序不可重复(HashSet、TreeSet)
- Map:键值对(HashMap、TreeMap)
java
import java.util.*;
public class CollectionDemo {
public static void main(String[] args) {
// ArrayList
List<String> names = new ArrayList<>();
names.add("张三");
names.add("李四");
names.add("王五");
names.remove("李四");
System.out.println(names); // [张三, 王五]
// HashSet
Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(2); // 重复,不会添加
System.out.println(numbers); // [1, 2]
// HashMap
Map<String, Integer> scores = new HashMap<>();
scores.put("张三", 95);
scores.put("李四", 88);
System.out.println(scores.get("张三")); // 95
// 遍历
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// Stream API
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Integer> evens = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evens); // [2, 4, 6, 8, 10]
}
}知识测验
第 1/5 题正确 0
Java 中 checked exception 和 unchecked exception 的区别是?
下一节
下一节 面向对象