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

相关推荐
程序员张33 分钟前
Maven编译和打包插件
java·spring boot·maven
ybq195133454311 小时前
Redis-主从复制-分布式系统
java·数据库·redis
weixin_472339462 小时前
高效处理大体积Excel文件的Java技术方案解析
java·开发语言·excel
小毛驴8502 小时前
Linux 后台启动java jar 程序 nohup java -jar
java·linux·jar
DKPT3 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
好奇的菜鸟4 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
DuelCode5 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
优创学社25 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
幽络源小助理5 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
猴哥源码5 小时前
基于Java+springboot 的车险理赔信息管理系统
java·spring boot