Java - 一篇带你了解类成员(基本数据类型/包装类/对象/数组)默认值

代码 

/**
 * @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

  • 只需记住基本数据类型默认值即可,特殊了解的已经在代码里标注 

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值