1. 程式人生 > >物件屬性和陣列元素的初始預設值

物件屬性和陣列元素的初始預設值

package initialvalue.one;

public class Main {
    public char charVal;
    public byte byteVal;
    public short shortVal;
    public int intVal;
    public long longVal;
    public float floatVal;
    public double doubleVal;
    public boolean booleanVal;

    public Character charObj;
    public
Byte byteObj; public Short shortObj; public Integer intObj; public Long longObj; public Float floatObj; public Double doubleObj; public Boolean booleanObj; @Override public String toString() { return "Main [charVal=" + charVal + ", byteVal=" + byteVal + ", shortVal="
+ shortVal + ", intVal=" + intVal + ", longVal=" + longVal + ", floatVal=" + floatVal + ", doubleVal=" + doubleVal + ", booleanVal=" + booleanVal + ", charObj=" + charObj + ", byteObj=" + byteObj + ", shortObj=" + shortObj + ", intObj=" + intObj + ", longObj="
+ longObj + ", floatObj=" + floatObj + ", doubleObj=" + doubleObj + ", booleanObj=" + booleanObj + "]"; } public static void main(String[] args) { System.out.println("物件屬性初始值..."); System.out.println(new Main().toString()); System.out.println("陣列元素初始值..."); char[] charArray=new char[3]; byte[] byteArray=new byte[3]; short[] shortArray=new short[3]; int[] intArray=new int[3]; long[] longArray=new long[3]; float[] floatArray=new float[3]; double[] doubleArray=new double[3]; boolean[] booleanArray=new boolean[3]; Integer[] intObjArray=new Integer[3]; System.out.print("charArray:["); for(char tmp: charArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("byteArray:["); for(byte tmp: byteArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("shortArray:["); for(short tmp: shortArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("intArray:["); for(int tmp: intArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("longArray:["); for(long tmp: longArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("floatArray:["); for(float tmp: floatArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("doubleArray:["); for(double tmp: doubleArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("booleanArray:["); for(boolean tmp: booleanArray){ System.out.print(tmp+","); } System.out.println("]"); System.out.print("intObjArray:["); for(Integer tmp: intObjArray){ System.out.print(tmp+","); } System.out.println("]"); /* * Output: * 物件屬性初始值... * Main [charVal= , byteVal=0, shortVal=0, intVal=0, longVal=0, floatVal=0.0, doubleVal=0.0, booleanVal=false, charObj=null, byteObj=null, shortObj=null, intObj=null, longObj=null, floatObj=null, doubleObj=null, booleanObj=null] * 陣列元素初始值... * charArray:[ , , ,] * byteArray:[0,0,0,] * shortArray:[0,0,0,] * intArray:[0,0,0,] * longArray:[0,0,0,] * floatArray:[0.0,0.0,0.0,] * doubleArray:[0.0,0.0,0.0,] * booleanArray:[false,false,false,] * intObjArray:[null,null,null,] */ } }

注:其實可以將陣列的各元素看做陣列物件的屬性,如此物件屬性和陣列元素初始預設值就簡化為物件屬性預設值,基本型別的預設值為0(其中浮點型和雙精度浮點型預設為0.0,字元型預設為空格,布林型預設為false),引用型別的預設值為null。