首页 文章资讯内容详情

Java中的数字和非数字的原始数据类型的默认值

2026-06-03 1 花语

在Java中创建实例变量时,您需要对其进行初始化,否则编译器将代表您使用默认值进行初始化-

byte:0

short:0

int:0

long:0

float:0.0

double:0.0

boolean:false

string:null

示例

在下面的Java程序中,将打印Java中数字和非数字基元变量的默认值。

public class DefaultValues { byte byteVariable; short shortVariable; int intVariable; long longVaraible; float floatVariable; double doubleVariable; boolean boolVariable; String stringVariable; public static void main(String args[]){ DefaultValues obj = new DefaultValues(); System.out.println("Java中数值变量的默认值:"); System.out.println("byte: "+obj.byteVariable); System.out.println("short: "+obj.shortVariable); System.out.println("int: "+obj.intVariable); System.out.println("long: "+obj.longVaraible); System.out.println("float: "+obj.floatVariable); System.out.println("double: "+obj.doubleVariable); System.out.println("Java中非数值变量的默认值:"); System.out.println("boolean: "+obj.boolVariable); System.out.println("string: "+obj.stringVariable); } }

输出结果

Java中数值变量的默认值: byte: 0 short: 0 int: 0 long: 0 float: 0.0 double: 0.0 Java中非数值变量的默认值: boolean: false string: null