### Java编写N个代码示例的标题

#### 1. 基础语法示例

```java

public class HelloWorld {

public static void main(String[] args) {

System.out.println(Hello, World!);

}

}

```

#### 2. 数据类型示例

```java

public class DataTypes {

public static void main(String[] args) {

int number = 10;

double decimal = 3.14;

char letter = 'A';

boolean flag = true;

String text = Java Programming;

System.out.println(整数: + number);

System.out.println(小数: + decimal);

System.out.println(字符: + letter);

System.out.println(布尔: + flag);

System.out.println(字符串: + text);

}

}

```

#### 3. 控制结构示例

```java

public class ControlStructures {

public static void main(String[] args) {

// if-else语句

int score = 85;

if (score >= 90) {

System.out.println(优秀);

} else if (score >= 60) {

System.out.println(及格);

} else {

System.out.println(不及格);

}

// for循环

for (int i = 1; i <= 5; i++) {

System.out.println(循环次数: + i);

}

// while循环

int count = 1;

while (count <= 3) {

System.out.println(计数: + count);

count++;

}

}

}

```

#### 4. 数组操作示例

```java

public class ArrayExample {

public static void main(String[] args) {

int[] numbers = {1, 2, 3, 4, 5};

// 遍历数组

for (int i = 0; i < numbers.length; i++) {

System.out.println(元素 + i + : + numbers[i]);

}

// 增强for循环

for (int num : numbers) {

System.out.println(数值: + num);

}

}

}

```

#### 5. 面向对象编程示例

```java

// 定义类

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(我叫 + name + ,今年 + age + 岁);

}

// getter和setter

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public int getAge() { return age; }

public void setAge(int age) { this.age = age; }

}

public class OOPExample {

public static void main(String[] args) {

Person person = new Person(张三, 25);

person.introduce();

}

}

```

#### 6. 异常处理示例

```java

public class ExceptionHandling {

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(程序执行完成);

}

}

public static int divide(int a, int b) {

return a / b;

}

}

```

#### 7. 集合框架示例

```java

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class CollectionExample {

public static void main(String[] args) {

// List示例

List list = new ArrayList<>();

list.add(苹果);

list.add(香蕉);

list.add(橙子);

for (String fruit : list) {

System.out.println(水果: + fruit);

}

// Map示例

Map map = new HashMap<>();

map.put(语文, 90);

map.put(数学, 95);

map.put(英语, 88);

for (Map.Entry entry : map.entrySet()) {

System.out.println(entry.getKey() + : + entry.getValue());

}

}

}

```

#### 8. 文件操作示例

```java

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class FileOperation {

public static void main(String[] args) {

// 写入文件

try {

FileWriter writer = new FileWriter(test.txt);

writer.write(这是测试文件内容 );

writer.write(第二行内容);

writer.close();

System.out.println(文件写入成功);

} catch (IOException e) {

System.out.println(文件写入失败: + e.getMessage());

}

// 读取文件

try {

File file = new File(test.txt);

Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

System.out.println(文件内容: + line);

}

scanner.close();

} catch (IOException e) {

System.out.println(文件读取失败: + e.getMessage());

}

}

}

```

#### 9. 多线程示例

```java

class MyThread extends Thread {

private String threadName;

public MyThread(String name) {

this.threadName = name;

}

@Override

public void run() {

for (int i = 1; i <= 3; i++) {

System.out.println(threadName + 执行第 + i + 次);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

System.out.println(线程中断);

}

}

}

}

public class ThreadExample {

public static void main(String[] args) {

MyThread thread1 = new MyThread(线程A);

MyThread thread2 = new MyThread(线程B);

thread1.start();

thread2.start();

}

}

```

#### 10. 网络编程示例

```java

import java.net.InetAddress;

import java.net.UnknownHostException;

public class NetworkExample {

public static void main(String[] args) {

try {

// 获取本地主机信息

InetAddress localHost = InetAddress.getLocalHost();

System.out.println(本地主机名: + localHost.getHostName());

System.out.println(本地IP地址: + localHost.getHostAddress());

// 获取远程主机信息

InetAddress google = InetAddress.getByName(www.google.com);

System.out.println(Google主机名: + google.getHostName());

System.out.println(Google IP地址: + google.getHostAddress());

} catch (UnknownHostException e) {

System.out.println(无法解析主机: + e.getMessage());

}

}

}

```

这些示例涵盖了Java编程的核心概念,从基础语法到高级特性,为学习和掌握Java编程提供了实用的参考。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐