要显示数据类型的默认初始值,您只需要声明一个相同数据类型的变量并显示它即可。
以下是一个Java程序,用于显示DataTypes的初始值。
public class Demo { boolean t; byte b; short s; int i; long l; float f; double d; void Display() { System.out.println("boolean (Initial Value) = " + t); System.out.println("byte (Initial Value) = " + b); System.out.println("short (Initial Value) = " + s); System.out.println("int (Initial Value) = " + i); System.out.println("long (Initial Value) = " + l); System.out.println("float (Initial Value) = " + f); System.out.println("double (Initial Value) = " + d); } public static void main(String[] args) { Demo d = new Demo(); System.out.println("Displaying initial values..."); d.Display(); } }
输出结果
Displaying initial values... boolean (Initial Value) = false byte (Initial Value) = 0 short (Initial Value) = 0 int (Initial Value) = 0 long (Initial Value) = 0 float (Initial Value) = 0.0 double (Initial Value) = 0.0
在上面的程序中,我们声明了一个具有不同数据类型的变量。
boolean t; byte b; short s; int i; long l; float f; double d;
现在,要获取默认的初始值,只需打印上面声明的变量即可。
System.out.println("boolean (Initial Value) = " + t); System.out.println("byte (Initial Value) = " + b); System.out.println("short (Initial Value) = " + s); System.out.println("int (Initial Value) = " + i); System.out.println("long (Initial Value) = " + l); System.out.println("float (Initial Value) = " + f); System.out.println("double (Initial Value) = " + d);
上面显示了默认值。