一、基础语法实例
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
这是一个典型的Java的Hello World程序,可以帮助初学者熟悉基本语法。其中,public表示访问权限,class定义类名,main方法为程序入口,String[] args表示接收命令行参数。
二、流程控制实例
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入n:");
int n = sc.nextInt();
int f1 = 1, f2 = 1;
for (int i = 1; i <= n; i++) {
System.out.print(f1 + " ");
int temp = f1;
f1 = f2;
f2 = temp + f2;
}
}
}
这个程序可以输出斐波那契数列,输入一个正整数n,输出前n项斐波那契数列。其中,利用Scanner类实现读入用户输入,然后使用for循环遍历输出。
三、面向对象实例
3.1 类定义实例
public class Student {
String name;
int age;
String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void display() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
}
}
这个例子定义了一个Student类,其中包含三个成员变量:name,age和gender。它还有一个构造函数和一个显示函数,构造函数用于初始化实例,显示函数用于在屏幕上显示学生的信息。
3.2 继承实例
public class Animal {
public void move() {
System.out.println("动物可以移动");
}
}
public class Dog extends Animal {
public void move() {
System.out.println("狗可以跑和走");
}
}
这个例子定义了一个Animal类和一个Dog类,Dog类继承自Animal类,重写了move()方法。其中,Animal类的move()方法用于描述动物可以移动,而Dog类的move()方法则专门描述了狗的移动方式。
3.3 接口实例
public interface Sport {
void run();
void jump();
}
public class Athlete implements Sport {
public void run() {
System.out.println("运动员跑步");
}
public void jump() {
System.out.println("运动员跳高");
}
}
这个例子定义了一个Sport接口,其中包含两个方法:run()和jump();另一个类Athlete实现了Sport接口,其中重载了run()和jump()方法。在Athlete类中,运动员可以使用run()来跑步,使用jump()来跳高。
四、多线程实例
4.1 创建线程实例
public class MyThread extends Thread {
public void run() {
System.out.println("线程开始运行:" + this.getName());
for (int i = 0; i < 5; i++) {
System.out.println(this.getName() + "运行:" + i);
try {
sleep((int) Math.random() * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程结束:" + this.getName());
}
}
public class TestThread {
public static void main(String[] args) {
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}
这个例子创建了一个MyThread继承自Thread的类,并且重载了run()方法,然后创建了两个MyThread实例,最后启动这两个线程。在程序运行时,两个线程会同时执行并打印出不同的运行次数。
4.2 线程同步实例
public class Account {
private int balance = 100;
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("存款" + amount + "\t余额:" + balance);
}
public synchronized void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
System.out.println("取款" + amount + "\t余额:" + balance);
} else {
System.out.println("余额不足");
}
}
}
public class DepositThread extends Thread {
private Account account;
public DepositThread(Account account) {
this.account = account;
}
public void run() {
for (int i = 0; i < 10; i++) {
account.deposit(10);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class WithdrawThread extends Thread {
private Account account;
public WithdrawThread(Account account) {
this.account = account;
}
public void run() {
for (int i = 0; i < 10; i++) {
account.withdraw(10);
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestAccount {
public static void main(String[] args) {
Account account = new Account();
new DepositThread(account).start();
new WithdrawThread(account).start();
}
}
这个例子演示了如何使用synchronized关键字实现线程的同步。其中,Account类代表一个银行账户,deposit()方法用于向账户中存入款项,withdraw()方法用于从账户中取款。DepositThread和WithdrawThread代表两个线程,分别进行存款和取款操作,这两个线程对应着同一个账户对象,保证了线程同步。