1. Hello World 程序
最简单的Java程序,用于输出 "Hello, World!"。
typescript
java
复制编辑
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2. 变量与数据类型
展示如何声明变量,并展示常见的基本数据类型。
ini
java
复制编辑
public class DataTypes {
public static void main(String[] args) {
int a = 10;
double b = 3.14;
char c = 'A';
boolean d = true;
String e = "Hello Java";
System.out.println("Integer: " + a);
System.out.println("Double: " + b);
System.out.println("Char: " + c);
System.out.println("Boolean: " + d);
System.out.println("String: " + e);
}
}
3. 控制结构:if-else 语句
根据条件输出不同的信息。
csharp
java
复制编辑
public class IfElseExample {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("The number is positive.");
} else if (num < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
4. 循环:for 和 while 循环
展示如何使用 for
和 while
循环打印数字。
for
循环示例:
arduino
java
复制编辑
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration " + i);
}
}
}
while
循环示例:
arduino
java
复制编辑
public class WhileLoopExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Iteration " + i);
i++;
}
}
}
5. 数组
演示如何创建和遍历数组。
csharp
java
复制编辑
public class ArrayExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// 使用for循环遍历数组
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
// 使用增强型for循环遍历数组
for (int num : numbers) {
System.out.println(num);
}
}
}
6. 方法的定义与调用
定义一个简单的方法并调用它。
typescript
java
复制编辑
public class MethodExample {
// 定义一个方法
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
// 调用方法
greet("Alice");
greet("Bob");
}
}
7. 类与对象
创建一个简单的类,并在主程序中实例化对象。
typescript
java
复制编辑
public class Car {
// 属性
String make;
String model;
// 方法
void displayInfo() {
System.out.println("Car make: " + make);
System.out.println("Car model: " + model);
}
public static void main(String[] args) {
// 创建对象
Car myCar = new Car();
myCar.make = "Toyota";
myCar.model = "Corolla";
// 调用方法
myCar.displayInfo();
}
}
8. 构造函数
展示如何在类中使用构造函数。
arduino
java
复制编辑
public class Person {
// 属性
String name;
int age;
// 构造函数
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// 方法
void introduce() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
}
public static void main(String[] args) {
// 创建对象时调用构造函数
Person person1 = new Person("Alice", 25);
person1.introduce();
Person person2 = new Person("Bob", 30);
person2.introduce();
}
}
9. 继承与多态
展示如何使用继承和多态。
java
java
复制编辑
// 父类
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
// 子类继承父类
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.sound(); // 输出 Animal makes a sound
Dog dog = new Dog();
dog.sound(); // 输出 Dog barks
// 多态示例
Animal myAnimal = new Dog();
myAnimal.sound(); // 输出 Dog barks
}
}
10. 异常处理:try-catch
使用 try-catch
语句处理异常。
csharp
java
复制编辑
public class ExceptionExample {
public static void main(String[] args) {
try {
int[] numbers = new int[2];
numbers[3] = 10; // 抛出 ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught exception: " + e.getMessage());
}
System.out.println("Program continues after exception handling.");
}
}
这些示例展示了 Java 中的一些基本概念和语法。如果你对某个部分有更多疑问或想了解更深入的内容,随时告诉我!