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

我就是我 2024-02-06 00:04 121阅读 0赞

对于类的成员变量

不管程序有没有显示的初始化,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.静态变量

  1. public class GlobalVar {
  2. public static char aChar;
  3. public static int anInt;
  4. public static long aLong;
  5. public static float aFloat;
  6. public static double aDouble;
  7. public static String string;
  8. public static boolean aBoolean;
  9. public static int[] ints;
  10. public static void main(String[] args){
  11. System.out.println("全局变量char默认值:"+aChar);
  12. System.out.println("全局变量int默认值:"+anInt);
  13. System.out.println("全局变量long默认值:"+aLong);
  14. System.out.println("全局变量float默认值:"+aFloat);
  15. System.out.println("全局变量double默认值:"+aDouble);
  16. System.out.println("全局变量string默认值:"+string);
  17. System.out.println("全局变量aBoolean默认值:"+aBoolean);
  18. System.out.println("全局变量ints默认值:"+ints);
  19. if(aChar == 0){
  20. System.out.println("全局变量char默认值为空");
  21. }else{
  22. System.out.println("全局变量char默认值不为空");
  23. }
  24. }
  25. }
  26. 全局变量char默认值:
  27. 全局变量int默认值:0
  28. 全局变量long默认值:0
  29. 全局变量float默认值:0.0
  30. 全局变量double默认值:0.0
  31. 全局变量string默认值:null
  32. 全局变量aBoolean默认值:false
  33. 全局变量ints默认值:null
  34. 全局变量char默认值为空

2.成员变量

  1. public class MembereVar {
  2. private char aChar;
  3. private int anInt;
  4. private long aLong;
  5. private float aFloat;
  6. private double aDouble;
  7. private boolean aBoolean;
  8. private String string;
  9. private int[] ints;
  10. public static void main(String[] args){
  11. MembereVar membereVar = new MembereVar();
  12. System.out.println("成员变量char默认值:"+membereVar.aChar);
  13. System.out.println("成员变量int默认值:"+membereVar.anInt);
  14. System.out.println("成员变量long默认值:"+membereVar.aLong);
  15. System.out.println("成员变量float默认值:"+membereVar.aFloat);
  16. System.out.println("成员变量double默认值:"+membereVar.aDouble);
  17. System.out.println("成员变量boolean默认值:"+membereVar.aBoolean);
  18. System.out.println("成员变量string默认值:"+membereVar.string);
  19. System.out.println("成员变量ints默认值:"+membereVar.ints);
  20. if(membereVar.aChar == 0){
  21. System.out.println("成员变量char默认值为空");
  22. }else{
  23. System.out.println("成员变量char默认值不为空");
  24. }
  25. }
  26. }
  27. 成员变量char默认值:
  28. 成员变量int默认值:0
  29. 成员变量long默认值:0
  30. 成员变量float默认值:0.0
  31. 成员变量double默认值:0.0
  32. 成员变量boolean默认值:false
  33. 成员变量string默认值:null
  34. 成员变量ints默认值:null
  35. 成员变量char默认值为空

结论:

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

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

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

    if(aChar == 0){

    1. System.out.println("成员变量char默认值为空");

    }else{

    1. System.out.println("成员变量char默认值不为空");

    }

  • 根据char是否为0进行判断:
    如果为true, 则char没有被人为赋值, 是系统的默认值
    如果为false, 则char已经被人为赋值.

3.局部变量

  1. public class LocalVar {
  2. public static void main(String[] args){
  3. char aChar ;
  4. int anInt;
  5. long aLong;
  6. float aFloat;
  7. double aDouble;
  8. boolean aBoolean;
  9. String string;
  10. int[] ints;
  11. System.out.println("局部变量char默认值:"+aChar);
  12. System.out.println("局部变量int默认值:"+anInt);
  13. System.out.println("局部变量long默认值:"+aLong);
  14. System.out.println("局部变量float默认值:"+aFloat);
  15. System.out.println("局部变量double默认值:"+aDouble);
  16. System.out.println("局部变量boolean默认值:"+aBoolean);
  17. System.out.println("局部变量string默认值:"+string);
  18. System.out.println("局部变量ints默认值:"+ints);
  19. }
  20. }
  21. Error:(17, 43) java: 可能尚未初始化变量aChar
  22. Error:(18, 42) java: 可能尚未初始化变量anInt
  23. Error:(19, 43) java: 可能尚未初始化变量aLong
  24. Error:(20, 44) java: 可能尚未初始化变量aFloat
  25. Error:(21, 45) java: 可能尚未初始化变量aDouble
  26. Error:(22, 46) java: 可能尚未初始化变量aBoolean
  27. Error:(23, 45) java: 可能尚未初始化变量string
  28. Error:(24, 43) java: 可能尚未初始化变量ints

结论:

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

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

  • 局部变量中的基本数组类型new之后的默认值和成员变量(也可以说是静态变量)的默认值是相同.(请看下图)

    public class LocalVar {

    1. public static void main(String[] args) {
    2. int[] ints;
    3. ints = new int[5];
    4. for (int i = 0; i < 5; i++) {
    5. System.out.print(ints[i] + " ");
    6. }
    7. System.out.println();
    8. float[] floats;
    9. floats = new float[5];
    10. for (int i = 0; i < 5; i++) {
    11. System.out.print(floats[i] + " ");
    12. }
    13. System.out.println();
    14. double[] doubles;
    15. doubles = new double[5];
    16. for (int i = 0; i < 5; i++) {
    17. System.out.print(doubles[i] + " ");
    18. }
    19. System.out.println();
    20. }

    }

    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



  1. /**
  2. * @author Lux Sun
  3. * @date 2021/9/13
  4. */
  5. public class DefaultValueDemo {
  6. private byte byteValue;
  7. private short shortValue;
  8. private int intValue;
  9. private long longValue;
  10. private float floatValue;
  11. private double doubleValue;
  12. private char charValue;
  13. private char[] charArrayValue;
  14. private boolean booleanValue;
  15. private boolean[] booleanArrayValue;
  16. private Byte byteVal;
  17. private Short shortVal;
  18. private Integer integerVal;
  19. private Long longVal;
  20. private Float floatVal;
  21. private Double doubleVal;
  22. private Character characterVal;
  23. private Boolean booleanVal;
  24. private Boolean[] booleanArrayVal;
  25. private String stringVal;
  26. private String[] stringArrayVal;
  27. private Object objectVal;
  28. public static void main(String[] args) {
  29. DefaultValueDemo demo = new DefaultValueDemo();
  30. System.out.println("byte: " + demo.byteValue); // (byte)0
  31. System.out.println("short: " + demo.shortValue); // (short)0
  32. System.out.println("int: " + demo.intValue);
  33. System.out.println("long: " + demo.longValue); // 0L
  34. System.out.println("float: " + demo.floatValue); // 0.0f
  35. System.out.println("double: " + demo.doubleValue); // 0.0d
  36. System.out.println("char: " + demo.charValue); // '\u0000' 什么也不输出, 不要认为输出是空格
  37. System.out.println("char[]: " + demo.charArrayValue);
  38. System.out.println("boolean: " + demo.booleanValue);
  39. System.out.println("Byte: " + demo.byteVal);
  40. System.out.println("Short: " + demo.shortVal);
  41. System.out.println("Integer: " + demo.integerVal);
  42. System.out.println("Long: " + demo.longVal);
  43. System.out.println("Float: " + demo.floatVal);
  44. System.out.println("Double: " + demo.doubleVal);
  45. System.out.println("Character: " + demo.characterVal);
  46. System.out.println("Boolean: " + demo.booleanVal);
  47. System.out.println("String: " + demo.stringVal);
  48. System.out.println("String[]: " + demo.stringArrayVal);
  49. System.out.println("Object: " + demo.objectVal);
  50. System.out.println("boolean[]: " + demo.booleanArrayValue);
  51. System.out.println("Boolean[]: " + demo.booleanArrayVal);
  52. }
  53. }
  54. byte: 0
  55. short: 0
  56. int: 0
  57. long: 0
  58. float: 0.0
  59. double: 0.0
  60. char:
  61. char[]: null
  62. boolean: false
  63. Byte: null
  64. Short: null
  65. Integer: null
  66. Long: null
  67. Float: null
  68. Double: null
  69. Character: null
  70. Boolean: null
  71. String: null
  72. String[]: null
  73. Object: null
  74. boolean[]: null
  75. Boolean[]: null

总结

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

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java基础:基本数据类型包装

    1. 基本类型包装类概述 为了方便操作基本数据类型值,将其封装成了对象,在对象中定义了属性和行为丰富了该数据的操作。用于描述该对象的类就称为基本数据类型对象包装类。

    相关 Java 基本数据类型包装

    【基本数据类型对象包装类】 想要对基本类型数据进行更多的操作,最方便的方式就是将其封装成对象。 为啥呢?因为在对象描述中就可以定义更多的属性和行为对该基本数据类型进行操作...