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

相关推荐
ademen2 小时前
spring4第6课-bean之间的关系+bean的作用范围
java·spring
cccl.2 小时前
Java在word中指定位置插入图片。
java·word
kingbal2 小时前
Elasticsearch:spring2.x集成elasticsearch8.x
java·spring2.x·elastic8.x
三两肉3 小时前
Java 中 ArrayList、Vector、LinkedList 的核心区别与应用场景
java·开发语言·list·集合
clk66075 小时前
SSM 框架核心知识详解(Spring + SpringMVC + MyBatis)
java·spring·mybatis
shangjg37 小时前
Kafka 的 ISR 机制深度解析:保障数据可靠性的核心防线
java·后端·kafka
Alan3168 小时前
Qt 中,设置事件过滤器(Event Filter)的方式
java·开发语言·数据库
小鹭同学_9 小时前
Java基础 Day28 完结篇
java·开发语言·log4j
椰椰椰耶10 小时前
[网页五子棋][匹配模块]实现胜负判定,处理玩家掉线
java·开发语言·spring boot·websocket·spring
on the way 12310 小时前
结构性设计模式之Flyweight(享元)
java·设计模式·享元模式