Java - 一篇带你了解类成员(基本数据类型/包装类/对象/数组)默认值
Posted 程序员牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
-
只需记住基本数据类型默认值即可,特殊了解的已经在代码里标注
以上是关于Java - 一篇带你了解类成员(基本数据类型/包装类/对象/数组)默认值的主要内容,如果未能解决你的问题,请参考以下文章