Files
home/docs/java/basic.md

116 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Java基础语法
## 变量与数据类型
Java是一种强类型语言每个变量都必须声明其类型。
### 基本数据类型
Java有8种基本数据类型
| 类型 | 关键字 | 大小 | 取值范围 |
|------|--------|------|----------|
| 字节型 | byte | 1字节 | -128 ~ 127 |
| 短整型 | short | 2字节 | -32768 ~ 32767 |
| 整型 | int | 4字节 | -2^31 ~ 2^31-1 |
| 长整型 | long | 8字节 | -2^63 ~ 2^63-1 |
| 单精度浮点 | float | 4字节 | 约±3.4E38 |
| 双精度浮点 | double | 8字节 | 约±1.7E308 |
| 字符型 | char | 2字节 | 0 ~ 65535 |
| 布尔型 | boolean | 1位 | true/false |
### 变量声明
```java
// 声明变量
int age = 25;
String name = "张三";
double salary = 10000.50;
boolean isActive = true;
// 常量
final double PI = 3.14159;
```
## 运算符
### 算术运算符
```java
int a = 10, b = 3;
System.out.println(a + b); // 13 加法
System.out.println(a - b); // 7 减法
System.out.println(a * b); // 30 乘法
System.out.println(a / b); // 3 除法
System.out.println(a % b); // 1 取余
```
### 比较运算符
```java
int x = 5, y = 10;
System.out.println(x == y); // false
System.out.println(x != y); // true
System.out.println(x > y); // false
System.out.println(x < y); // true
```
## 流程控制
### 条件语句
```java
int score = 85;
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
```
### 循环语句
```java
// for循环
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// while循环
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
// 增强for循环
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.println(num);
}
```
## 数组
```java
// 声明数组
int[] numbers = new int[5];
String[] names = {"张三", "李四", "王五"};
// 访问数组元素
numbers[0] = 10;
System.out.println(names[1]); // 李四
// 数组长度
System.out.println(names.length); // 3
```
## 验证自动化部署
> 2026-05-07: 此文档已通过 CI/CD 自动化部署验证
>
> 第四次验证 - webhook 已改为内网地址