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

相关推荐
野犬寒鸦2 小时前
多级缓存架构:性能与数据一致性的平衡处理(原理及优势详解+项目实战)
java·服务器·redis·后端·缓存
帧栈4 小时前
开发避坑指南(58):Java Stream 按List元素属性分组实战指南
java
Da Da 泓4 小时前
LinkedList模拟实现
java·开发语言·数据结构·学习·算法
海琴烟Sunshine4 小时前
Leetcode 14. 最长公共前缀
java·服务器·leetcode
城管不管5 小时前
Lambda
java
龙茶清欢5 小时前
5、urbane-commerce 微服务统一依赖版本管理规范
java·运维·微服务
海琴烟Sunshine7 小时前
Leetcode 26. 删除有序数组中的重复项
java·算法·leetcode
RoboWizard7 小时前
移动固态硬盘连接手机无法读取是什么原因?
java·spring·智能手机·电脑·金士顿
笨蛋不要掉眼泪8 小时前
SpringBoot项目Excel成绩录入功能详解:从文件上传到数据入库的全流程解析
java·vue.js·spring boot·后端·spring·excel
wshzrf8 小时前
【Java系列课程·Java学前须知】第3课 JDK,JVM,JRE的区别和优缺
java·开发语言·jvm