探索 Java 基本数据类型

Beginner

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习 Java 中的八种基本数据类型(primitive data types)。你将了解它们的大小、范围和默认值。你还将学习自动装箱(autoboxing)的概念。

创建一个新的 Java 文件

使用以下命令创建一个新的 Java 文件,并将其命名为 PrimitivesLab.java

touch PrimitivesLab.java

声明 int 和 byte 数据类型

声明一个名为 i 的 int 数据类型和一个名为 b 的 byte 数据类型,并分别赋值为 1510

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
    }
}

声明 short 和 long 数据类型

声明一个名为 s 的 short 数据类型并赋值为 1000。声明一个名为 l 的 long 数据类型并赋值为 9999999L

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
    }
}

声明 float 和 double 数据类型

声明一个名为 f 的 float 数据类型并赋值为 3.14f。声明一个名为 d 的 double 数据类型并赋值为 2.71828

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
    }
}

声明 boolean 和 char 数据类型

声明一个名为 bool 的 boolean 数据类型并赋值为 true。声明一个名为 c 的 char 数据类型并赋值为 'A'

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
    }
}

演示自动装箱(autoboxing)

通过声明一个名为 x 的 Integer 数据类型并赋值为 25,来演示自动装箱(autoboxing)。

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        //Print the values
        System.out.println("Value of i: " + i);
        System.out.println("Value of b: " + b);
        System.out.println("Value of s: " + s);
        System.out.println("Value of l: " + l);
        System.out.println("Value of f: " + f);
        System.out.println("Value of d: " + d);
        System.out.println("Value of bool: " + bool);
        System.out.println("Value of c: " + c);
        System.out.println("Value of x: " + x);
    }
}

演示整数下溢(integer underflow)

声明一个名为 n 的整数数据类型并赋值为 2147483647。将 n 增加 1 并观察输出结果。

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
    }
}

演示整数上溢(integer overflow)

声明一个名为 m 的整数数据类型并赋值为 -2147483648。将 m 减少 1 并观察输出结果。

public class PrimitivesLab {
    public static void main(String[] args){
        int i = 15;
        byte b = 10;
        short s = 1000;
        long l = 9999999L;
        float f = 3.14f;
        double d = 2.71828;
        boolean bool = true;
        char c = 'A';
        Integer x = 25;
        int n = 2147483647;
        n = n + 1;
        System.out.println("Value is: " + n);
        int m = -2147483648;
        m = m - 1;
        System.out.println("Value is: " + m);
    }
}

编译并运行程序

在你的终端中使用以下命令编译程序:

javac PrimitivesLab.java

使用以下命令运行程序:

java PrimitivesLab

总结

在本实验中,你学习了 Java 中的八种基本数据类型。你创建了一个 Java 程序,并声明了每种基本数据类型的变量。你了解了自动装箱(autoboxing)以及 Java 如何自动将基本数据类型转换为其对应的包装类。你还学习了整数数据类型中的上溢(overflow)和下溢(underflow)。最后,你编译并运行了程序以查看输出结果。你可以在编程中使用这些数据类型来高效地存储和操作数据。

您可能感兴趣的其他 教程