Java基本数据类型/包装类/对象/数组默认值

Java基本数据类型/包装类/对象/数组默认值不管程序有没有显示的初始化 Java 虚拟机都会先自动给它初始化为默认值

大家好,欢迎来到IT知识分享网。

对于类的成员变量

不管程序有没有显示的初始化,Java 虚拟机都会先自动给它初始化为默认值。

1、整数类型(byte、short、int、long)的基本类型变量的默认值为0。

2、单精度浮点型(float)的基本类型变量的默认值为0.0f。

3、双精度浮点型(double)的基本类型变量的默认值为0.0d。

4、字符型(char)的基本类型变量的默认为 “/u0000”。

5、布尔性的基本类型变量的默认值为 false。

6、引用类型的变量是默认值为 null。

7、数组引用类型的变量的默认值为 null。当数组变量的实例后,如果没有没有显示的为每个元素赋值,Java 就会把该数组的所有元素初始化为其相应类型的默认值。

数组例子:

  1. int[] a; //声明,没有初始化默认值是null
  2. int[] a=new int[5]; //初始化为默认值,int型为0

局部变量初始化

局部变量声明以后,Java 虚拟机不会自动的为它初始化为默认值。

因此对于局部变量,必须先经过显示的初始化,才能使用它。

如果编译器确认一个局部变量在使用之前可能没有被初始化,编译器将报错。


静态变量,成员变量,局部变量的默认值

1.静态变量

public class GlobalVar { 
     public static char aChar; public static int anInt; public static long aLong; public static float aFloat; public static double aDouble; public static String string; public static boolean aBoolean; public static int[] ints; public static void main(String[] args){ 
     System.out.println("全局变量char默认值:"+aChar); System.out.println("全局变量int默认值:"+anInt); System.out.println("全局变量long默认值:"+aLong); System.out.println("全局变量float默认值:"+aFloat); System.out.println("全局变量double默认值:"+aDouble); System.out.println("全局变量string默认值:"+string); System.out.println("全局变量aBoolean默认值:"+aBoolean); System.out.println("全局变量ints默认值:"+ints); if(aChar == 0){ 
     System.out.println("全局变量char默认值为空"); }else{ 
     System.out.println("全局变量char默认值不为空"); } } } 全局变量char默认值: 全局变量int默认值:0 全局变量long默认值:0 全局变量float默认值:0.0 全局变量double默认值:0.0 全局变量string默认值:null 全局变量aBoolean默认值:false 全局变量ints默认值:null 全局变量char默认值为空 

2.成员变量

public class MembereVar { 
     private char aChar; private int anInt; private long aLong; private float aFloat; private double aDouble; private boolean aBoolean; private String string; private int[] ints; public static void main(String[] args){ 
     MembereVar membereVar = new MembereVar(); System.out.println("成员变量char默认值:"+membereVar.aChar); System.out.println("成员变量int默认值:"+membereVar.anInt); System.out.println("成员变量long默认值:"+membereVar.aLong); System.out.println("成员变量float默认值:"+membereVar.aFloat); System.out.println("成员变量double默认值:"+membereVar.aDouble); System.out.println("成员变量boolean默认值:"+membereVar.aBoolean); System.out.println("成员变量string默认值:"+membereVar.string); System.out.println("成员变量ints默认值:"+membereVar.ints); if(membereVar.aChar == 0){ 
     System.out.println("成员变量char默认值为空"); }else{ 
     System.out.println("成员变量char默认值不为空"); } } } 成员变量char默认值: 成员变量int默认值:0 成员变量long默认值:0 成员变量float默认值:0.0 成员变量double默认值:0.0 成员变量boolean默认值:false 成员变量string默认值:null 成员变量ints默认值:null 成员变量char默认值为空 

结论:

  • 静态变量和成员变量的默认值是一样的
  • 基本数据类型: int = 0, long =0, float = 0.0, double = 0.0, boolean=false
  • 对象数据类型: 默认值都为null

注意: char这个基本数据类型

  • char的默认字符是空, 类似于两个单引号中没有任何字符, 但是在代码中
    是不能给字符串赋值为’’(如 char ch = ‘’), 如果这样写编译会报错, 因此我们就
    不能通过 if(ch == ‘’) 这种形式判断, 这种形式同样会报错.

  • 当我们想要判断char字符是不是被人为的赋值, 我们可以这样写:
if(aChar == 0){ 
     System.out.println("成员变量char默认值为空"); }else{ 
     System.out.println("成员变量char默认值不为空"); } 
  • 根据char是否为0进行判断:
    如果为true, 则char没有被人为赋值, 是系统的默认值
    如果为false, 则char已经被人为赋值.

3.局部变量

public class LocalVar { 
     public static void main(String[] args){ 
     char aChar ; int anInt; long aLong; float aFloat; double aDouble; boolean aBoolean; String string; int[] ints; System.out.println("局部变量char默认值:"+aChar); System.out.println("局部变量int默认值:"+anInt); System.out.println("局部变量long默认值:"+aLong); System.out.println("局部变量float默认值:"+aFloat); System.out.println("局部变量double默认值:"+aDouble); System.out.println("局部变量boolean默认值:"+aBoolean); System.out.println("局部变量string默认值:"+string); System.out.println("局部变量ints默认值:"+ints); } } Error:(17, 43) java: 可能尚未初始化变量aChar Error:(18, 42) java: 可能尚未初始化变量anInt Error:(19, 43) java: 可能尚未初始化变量aLong Error:(20, 44) java: 可能尚未初始化变量aFloat Error:(21, 45) java: 可能尚未初始化变量aDouble Error:(22, 46) java: 可能尚未初始化变量aBoolean Error:(23, 45) java: 可能尚未初始化变量string Error:(24, 43) java: 可能尚未初始化变量ints 

结论:

  • 局部变量系统默认不会给你默认值, 如果想要使用局部变量则必须进行初始化.

注意: 局部变量的数组new了之后就又有默认值.

  • 局部变量中的基本数组类型new之后的默认值和成员变量(也可以说是静态变量)的默认值是相同.(请看下图)
public class LocalVar { 
     public static void main(String[] args) { 
     int[] ints; ints = new int[5]; for (int i = 0; i < 5; i++) { 
     System.out.print(ints[i] + " "); } System.out.println(); float[] floats; floats = new float[5]; for (int i = 0; i < 5; i++) { 
     System.out.print(floats[i] + " "); } System.out.println(); double[] doubles; doubles = new double[5]; for (int i = 0; i < 5; i++) { 
     System.out.print(doubles[i] + " "); } System.out.println(); } } 0 0 0 0 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 


/ * @author Lux Sun * @date 2021/9/13 */ public class DefaultValueDemo { 
       private byte byteValue; private short shortValue; private int intValue; private long longValue; private float floatValue; private double doubleValue; private char charValue; private char[] charArrayValue; private boolean booleanValue; private boolean[] booleanArrayValue; private Byte byteVal; private Short shortVal; private Integer integerVal; private Long longVal; private Float floatVal; private Double doubleVal; private Character characterVal; private Boolean booleanVal; private Boolean[] booleanArrayVal; private String stringVal; private String[] stringArrayVal; private Object objectVal; public static void main(String[] args) { 
       DefaultValueDemo demo = new DefaultValueDemo(); System.out.println("byte: " + demo.byteValue); // (byte)0 System.out.println("short: " + demo.shortValue); // (short)0 System.out.println("int: " + demo.intValue); System.out.println("long: " + demo.longValue); // 0L System.out.println("float: " + demo.floatValue); // 0.0f System.out.println("double: " + demo.doubleValue); // 0.0d System.out.println("char: " + demo.charValue); // '\u0000' 什么也不输出, 不要认为输出是空格 System.out.println("char[]: " + demo.charArrayValue); System.out.println("boolean: " + demo.booleanValue); System.out.println("Byte: " + demo.byteVal); System.out.println("Short: " + demo.shortVal); System.out.println("Integer: " + demo.integerVal); System.out.println("Long: " + demo.longVal); System.out.println("Float: " + demo.floatVal); System.out.println("Double: " + demo.doubleVal); System.out.println("Character: " + demo.characterVal); System.out.println("Boolean: " + demo.booleanVal); System.out.println("String: " + demo.stringVal); System.out.println("String[]: " + demo.stringArrayVal); System.out.println("Object: " + demo.objectVal); System.out.println("boolean[]: " + demo.booleanArrayValue); System.out.println("Boolean[]: " + demo.booleanArrayVal); } } 
byte: 0 short: 0 int: 0 long: 0 float: 0.0 double: 0.0 char: char[]: null boolean: false Byte: null Short: null Integer: null Long: null Float: null Double: null Character: null Boolean: null String: null String[]: null Object: null boolean[]: null Boolean[]: null 

总结

  • 所有对象、数组、包装类默认值:null
  • 只需记住基本数据类型默认值即可,特殊了解的已经在代码里标注

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/127888.html

(0)
上一篇 2025-09-04 16:33
下一篇 2025-09-04 16:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信