Java 基本语法详细教程

Java 是一种面向对象的编程语言,具有简单、可移植、安全等特性。下面我将详细介绍 Java 的基本语法。

1. Java 程序结构

一个最简单的 Java 程序如下:

1
2
3
4
5
6
7
8
// 定义类
public class HelloWorld {
// 主方法,程序入口
public static void main(String[] args) {
// 输出语句
System.out.println("Hello, World!");
}
}
  • public class HelloWorld:定义一个公共类,类名必须与文件名一致
  • public static void main(String[] args):主方法,程序执行的入口
  • System.out.println():输出语句

2. 基本数据类型

Java 有 8 种基本数据类型:

数据类型 大小 范围 默认值
byte 8位 -128到127 0
short 16位 -32,768到32,767 0
int 32位 -2^31到2^31-1 0
long 64位 -2^63到2^63-1 0L
float 32位 IEEE 754浮点数 0.0f
double 64位 IEEE 754浮点数 0.0d
char 16位 Unicode字符 ‘\u0000’
boolean 1位 true/false false

示例:

1
2
3
4
int age = 25;
double price = 19.99;
char grade = 'A';
boolean isJavaFun = true;

3. 变量与常量

变量声明与初始化

1
2
3
4
5
6
7
8
// 声明变量
int count;

// 初始化变量
count = 10;

// 声明并初始化
double pi = 3.14159;

常量(使用 final 关键字)

1
2
final double PI = 3.1415926;
final int MAX_VALUE = 100;

4. 运算符

算术运算符

1
2
3
4
5
6
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 (取余)

关系运算符

1
2
3
System.out.println(a > b);  // true
System.out.println(a == b); // false
System.out.println(a != b); // true

逻辑运算符

1
2
3
4
boolean x = true, y = false;
System.out.println(x && y); // false
System.out.println(x || y); // true
System.out.println(!x); // false

赋值运算符

1
2
3
int c = 5;
c += 3; // 等同于 c = c + 3
System.out.println(c); // 8

5. 控制结构

if-else 语句

1
2
3
4
5
6
7
8
9
10
11
int score = 85;

if (score >= 90) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}

switch 语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int day = 3;
String dayName;

switch (day) {
case 1:
dayName = "星期一";
break;
case 2:
dayName = "星期二";
break;
case 3:
dayName = "星期三";
break;
default:
dayName = "未知";
}
System.out.println(dayName); // 星期三

for 循环

1
2
3
for (int i = 0; i < 5; i++) {
System.out.println("i = " + i);
}

while 循环

1
2
3
4
5
int j = 0;
while (j < 5) {
System.out.println("j = " + j);
j++;
}

do-while 循环

1
2
3
4
5
int k = 0;
do {
System.out.println("k = " + k);
k++;
} while (k < 5);

6. 数组

一维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 声明并初始化数组
int[] numbers = {1, 2, 3, 4, 5};

// 访问数组元素
System.out.println(numbers[0]); // 1

// 遍历数组
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

// 增强for循环
for (int num : numbers) {
System.out.println(num);
}

多维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 二维数组
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// 访问元素
System.out.println(matrix[1][2]); // 6

// 遍历二维数组
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}

7. 方法(函数)

方法定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 无返回值方法
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}

// 有返回值方法
public static int add(int a, int b) {
return a + b;
}

// 方法重载
public static double add(double a, double b) {
return a + b;
}

方法调用

1
2
3
4
5
6
7
greet("Alice"); // 调用无返回值方法

int sum = add(5, 3); // 调用有返回值方法
System.out.println(sum); // 8

double dSum = add(2.5, 3.7);
System.out.println(dSum); // 6.2

8. 类和对象

类定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Person {
// 字段(属性)
String name;
int age;

// 构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// 方法
public void introduce() {
System.out.println("我叫" + name + ",今年" + age + "岁。");
}
}

创建对象

1
2
Person person1 = new Person("张三", 25);
person1.introduce(); // 我叫张三,今年25岁。

9. 包和导入

创建包

1
2
3
4
5
package com.example.myapp;

public class MyClass {
// 类内容
}

导入其他包中的类

1
2
import java.util.Scanner;
import java.util.*; // 导入java.util包中的所有类

10. 异常处理

1
2
3
4
5
6
7
try {
int result = 10 / 0; // 除以零会抛出ArithmeticException
} catch (ArithmeticException e) {
System.out.println("除数不能为零");
} finally {
System.out.println("这段代码总是会执行");
}

11. 输入输出

控制台输入

1
2
3
4
5
6
7
8
9
import java.util.Scanner;

Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的姓名: ");
String name = scanner.nextLine();
System.out.print("请输入您的年龄: ");
int age = scanner.nextInt();
System.out.println("您好," + name + "! 您今年" + age + "岁。");
scanner.close();

控制台输出

1
2
3
System.out.print("不换行输出");
System.out.println("换行输出");
System.out.printf("格式化输出: %s %d %.2f", "字符串", 100, 3.14159);

12. 字符串操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
String str1 = "Hello";
String str2 = "World";

// 字符串连接
String result = str1 + " " + str2;
System.out.println(result); // Hello World

// 字符串长度
System.out.println(result.length()); // 11

// 字符串比较
System.out.println(str1.equals("Hello")); // true
System.out.println(str1.equalsIgnoreCase("hello")); // true

// 子字符串
System.out.println(result.substring(0, 5)); // Hello

// 字符串分割
String[] parts = result.split(" ");
for (String part : parts) {
System.out.println(part);
}

以上是 Java 基本语法的详细内容。掌握这些基础知识后,你可以进一步学习 Java 的面向对象特性、集合框架、多线程等高级特性。

数学公式

最大值:Math.max(x,y)

1
Math.max(5, 10);//找5,10的最大值

最小值:Math.min(x,y)

1
Math.min(5, 10);//找5,10的最小值

平方根:Math.sqrt(x)

1
Math.sqrt(64);//64的平方根

绝对值:Math.abs(x)

1
Math.abs(-4.7);

生成随机数:Random Numbers

1
int randomNum = (int)(Math.random() * 101);  // 0 到 100

完整的[数学公式](Java Math 方法)