Java基础一文速过
Java基础一文速过
- 一、Java 简介
- 二、Java 基础语法
- 1. 基本结构
- 2. 数据类型
- 3. 变量与常量
- 4. 运算符
- 5. 控制语句
- 三、数组与方法
- 1. 数组
- 2. 方法(函数)
- 四、面向对象编程(OOP)
- 1. 类与对象
- 2. 封装(Encapsulation)
- 3. 继承(Inheritance)
- 4. 多态(Polymorphism)
- 5. 抽象类与接口
- 五、常用类与字符串
- 1. String
- 2. StringBuilder
- 3. 包装类(Wrapper)
- 六、异常处理
- 七、集合框架(Collection Framework)
- 八、输入与输出(IO)
- 1. 控制台输入
- 2. 文件操作
- 九、多线程(入门)
一、Java 简介
Java 是一种跨平台的、面向对象的高级编程语言,由 Sun 公司于 1995 年推出,目前由 Oracle 维护。
它具备以下特点:
- 跨平台性:通过 JVM(Java Virtual Machine)实现“一次编写,到处运行”。
- 面向对象:支持类、继承、多态、封装。
- 安全性高:代码运行在虚拟机沙盒中。
- 丰富的类库:提供了大量标准库支持网络、IO、集合等功能。
Java 程序从源码到运行的流程:
.java (源文件) → javac 编译 → .class (字节码) → JVM 解释执行
二、Java 基础语法
1. 基本结构
public class HelloWorld {public static void main(String[] args) {System.out.println("Hello, Java 25!");}
}
解释:
public class:定义一个公共类。main方法:Java 程序入口。System.out.println():打印输出。
2. 数据类型
基本数据类型(8种)
| 类型 | 字节数 | 默认值 | 范围 |
|---|---|---|---|
| byte | 1 | 0 | -128~127 |
| short | 2 | 0 | -32768~32767 |
| int | 4 | 0 | -2³¹~2³¹-1 |
| long | 8 | 0L | -2⁶³~2⁶³-1 |
| float | 4 | 0.0f | 单精度小数 |
| double | 8 | 0.0d | 双精度小数 |
| char | 2 | ‘\u0000’ | 单个字符 |
| boolean | 1 | false | true / false |
引用数据类型:类(Class)、接口(Interface)、数组(Array)、枚举(Enum)等。
3. 变量与常量
int age = 20; // 变量
final double PI = 3.14; // 常量
关键字 final 表示该变量不可修改。
4. 运算符
包括:
- 算术运算符:
+ - * / % - 比较运算符:
> < == != >= <= - 逻辑运算符:
&& || ! - 自增自减:
++ -- - 三元运算符:
(条件) ? 值1 : 值2
5. 控制语句
if (score >= 60) {System.out.println("及格");
} else {System.out.println("不及格");
}switch (day) {case 1 -> System.out.println("Monday");case 2 -> System.out.println("Tuesday");default -> System.out.println("Other day");
}
switch 在 Java 14+ 可使用箭头语法。
循环语句:
for (int i = 0; i < 5; i++) {System.out.println(i);
}while (condition) { ... }do { ... } while (condition);
三、数组与方法
1. 数组
int[] arr = {1, 2, 3};
System.out.println(arr[0]);
二维数组:
int[][] matrix = {{1, 2},{3, 4}
};
2. 方法(函数)
public static int add(int a, int b) {return a + b;
}
- 方法可带参数和返回值。
static表示静态方法,可直接通过类名调用。
方法重载(Overloading):
void print(int a) {}
void print(String s) {}
同名方法参数不同即可。
四、面向对象编程(OOP)
1. 类与对象
class Person {String name;int age;void sayHello() {System.out.println("Hello, I'm " + name);}
}
Person p = new Person();
p.name = "Tom";
p.sayHello();
2. 封装(Encapsulation)
class Student {private int score;public void setScore(int s) {if (s >= 0 && s <= 100)score = s;}public int getScore() {return score;}
}
通过 getter/setter 控制访问。
3. 继承(Inheritance)
class Animal {void eat() { System.out.println("eating"); }
}class Dog extends Animal {void bark() { System.out.println("barking"); }
}
4. 多态(Polymorphism)
Animal a = new Dog();
a.eat(); // 调用子类方法
多态实现依赖继承 + 方法重写(Override)。
5. 抽象类与接口
abstract class Shape {abstract void draw();
}interface Flyable {void fly();
}
类可实现多个接口:
class Bird implements Flyable {public void fly() { System.out.println("Flying"); }
}
五、常用类与字符串
1. String
String s = "Hello";
System.out.println(s.length());
System.out.println(s.toUpperCase());
字符串不可变,修改会产生新对象。
2. StringBuilder
用于频繁修改字符串内容:
StringBuilder sb = new StringBuilder("Java");
sb.append(" 25");
System.out.println(sb.toString());
3. 包装类(Wrapper)
基本类型 → 对象类型
int n = 5;
Integer obj = n; // 自动装箱
int m = obj; // 自动拆箱
六、异常处理
try {int a = 10 / 0;
} catch (ArithmeticException e) {System.out.println("除数不能为0");
} finally {System.out.println("程序结束");
}
自定义异常:
class MyException extends Exception {public MyException(String msg) {super(msg);}
}
七、集合框架(Collection Framework)
常用接口:
List:有序、可重复(如ArrayList、LinkedList)Set:无序、不重复(如HashSet、TreeSet)Map:键值对映射(如HashMap、TreeMap)
List<String> list = new ArrayList<>();
list.add("A");
list.add("B");for (String s : list) {System.out.println(s);
}Map<String, Integer> map = new HashMap<>();
map.put("Java", 25);
map.put("Python", 30);
System.out.println(map.get("Java"));
八、输入与输出(IO)
1. 控制台输入
import java.util.Scanner;Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String name = sc.nextLine();
System.out.println("你好," + name);
2. 文件操作
import java.io.FileWriter;
import java.io.FileReader;FileWriter fw = new FileWriter("test.txt");
fw.write("Hello Java!");
fw.close();FileReader fr = new FileReader("test.txt");
int c;
while ((c = fr.read()) != -1) {System.out.print((char)c);
}
fr.close();
九、多线程(入门)
class MyThread extends Thread {public void run() {System.out.println(Thread.currentThread().getName() + " running");}
}public class Test {public static void main(String[] args) {new MyThread().start();new MyThread().start();}
}
或者使用 Runnable 接口:
Runnable task = () -> System.out.println("Running...");
new Thread(task).start();
