Java 的基础代码示例

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 循环

展示如何使用 forwhile 循环打印数字。

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 中的一些基本概念和语法。如果你对某个部分有更多疑问或想了解更深入的内容,随时告诉我!

相关推荐
nanxun8862 小时前
记一次诡异的 Docker 容器"串包"故障排查
java
用户1563068103515 小时前
Day01 | Java 基础(Java SE)
java
行者全栈架构师6 小时前
Maven dependency:tree 的 8 个高级用法
java·后端
行者全栈架构师10 小时前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_011 小时前
mac(m5)平台编译openjdk
java
唐青枫1 天前
Java JDBC 实战指南:从 Connection 到事务和连接池
java
一个做软件开发的牛马1 天前
MyBatis-Plus 从零实战:完整搭建可运行 Demo,BaseMapper 零 SQL、Wrapper 条件构造、分页插件与代码生成器详解
java·后端
用户3721574261351 天前
Java 处理 PDF 图片:提取 PDF 中的图片,并压缩 PDF 图片体积
java
用户3721574261351 天前
Java 打印 Word 文档:从基础打印到高级设置
java
用户3521802454752 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程