Java 快速入门指南
1. 安装 Java
- 从 Oracle 官方网站 下载并安装最新版本的 Java Development Kit (JDK)。
- 安装集成开发环境 (IDE),如 IntelliJ IDEA、Eclipse 或 NetBeans。
2. 基本语法
2.1 变量和数据类型
Java 支持多种数据类型,如整数、浮点数、字符、字符串和布尔值。以下是一些示例代码:
public class Main {
public static void main(String[] args) {
// 数字
int a = 10;
double b = 3.14;
// 字符
char c = 'A';
// 字符串
String name = "Alice";
// 布尔值
boolean isActive = true;
// 数组
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Name: " + name);
}
}
2.2 基本操作
public class Main {
public static void main(String[] args) {
int a = 10;
double b = 3.14;
// 算术运算
int sum = a + (int)b;
int difference = a - (int)b;
double product = a * b;
double quotient = a / b;
// 字符串操作
String name = "Alice";
String greeting = "Hello, " + name;
// 数组操作
int[] numbers = {1, 2, 3, 4, 5};
numbers[0] = 10;
System.out.println("Sum: " + sum);
System.out.println(greeting);
}
}
3. 控制结构
3.1 条件语句
Java 使用 if
、else if
和 else
进行条件判断:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 5;
if (a > b) {
System.out.println("a is greater than b");
} else if (a == b) {
System.out.println("a is equal to b");
} else {
System.out.println("a is less than b");
}
}
}
3.2 循环
Java 支持 for
、while
和 do-while
循环:
public class Main {
public static void main(String[] args) {
// for 循环
for (int i = 0; i < 5; i++) {
System.out.println("i: " + i);
}
// while 循环
int count = 0;
while (count < 5) {
System.out.println("count: " + count);
count++;
}
// do-while 循环
int num = 0;
do {
System.out.println("num: " + num);
num++;
} while (num < 5);
}
}
4. 函数
函数用 return_type
和 function_name
定义:
public class Main {
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println("Sum: " + sum);
}
public static int add(int x, int y) {
return x + y;
}
}
5. 类和对象
Java 是面向对象的编程语言,可以定义类和创建对象:
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("My name is " + name + " and I am " + age + " years old.");
}
public static void main(String[] args) {
Person alice = new Person("Alice", 25);
alice.introduce();
}
}
6. 继承和多态
Java 支持继承和多态,允许子类继承父类的方法和属性,并可以重写方法:
class Animal {
public void makeSound() {
System.out.println("Some sound...");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound();
}
}
7. 接口和抽象类
接口和抽象类提供了更高的抽象层次,允许定义方法而不实现它们:
7.1 接口
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound();
}
}
7.2 抽象类
abstract class Animal {
public abstract void makeSound();
public void sleep() {
System.out.println("Zzz");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.makeSound();
myDog.sleep();
}
}
8. 集合框架
Java 提供了丰富的集合框架,包括 ArrayList
、HashMap
等:
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// ArrayList
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
System.out.println(names.get(0));
// HashMap
HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
System.out.println(ages.get("Alice"));
}
}
9. 异常处理
使用 try
、catch
和 finally
处理异常:
public class Main {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int c = a / b;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("This will always execute.");
}
}
}
10. 文件操作
Java 提供了丰富的文件操作支持:
import java.io.*;
public class Main {
public static void main(String[] args) {
// 写入文件
try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) {
writer.write("Hello, world!");
} catch (IOException e) {
e.printStackTrace();
}
// 读取文件
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
这份文档涵盖了 Java 的基础知识和常用操作。通过不断练习和参考官方文档,你将能够掌握 Java 并应用于各种项目中。
有关更多详细信息,请参考 Java 官方文档。