首页 > 代码库 > java基本类型作为成员变量时的初始值

java基本类型作为成员变量时的初始值

 

package primitivetypedefaultvalue;public class ListDefaultValue {    public static void main(String[] args) {        PrimitiveType pt=new PrimitiveType();        System.out.println(pt.toString());    }}class PrimitiveType{    @Override    public String toString() {        StringBuilder pt=new StringBuilder();        pt.append("age(int):")        .append(this.getAge())        .append("\n")        .append("moneyL:")        .append(this.getMoneyL())        .append("\n")        .append("moneyF:")        .append(this.getMoneyF())        .append("\n")        .append("moneyD:")        .append(this.getMoneyD())        .append("\n")        .append("sex (char):")        .append(this.getSex())        .append("; ")        .append("length(original):")        .append(String.valueOf(this.getSex()).length())        .append("; ")        .append("length(after trim()):")        .append(String.valueOf(this.getSex()).trim().length())        .append("\n")        .append("retire(boolean):")        .append(this.isRetire())        .append("\n")                ;        return pt.toString();    }        public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public long getMoneyL() {        return moneyL;    }    public void setMoneyL(long moneyL) {        this.moneyL = moneyL;    }    public float getMoneyF() {        return moneyF;    }    public void setMoneyF(float moneyF) {        this.moneyF = moneyF;    }    public double getMoneyD() {        return moneyD;    }    public void setMoneyD(double moneyD) {        this.moneyD = moneyD;    }    public char getSex() {        return sex;    }    public void setSex(char sex) {        this.sex = sex;    }    public boolean isRetire() {        return retire;    }    public void setRetire(boolean retire) {        this.retire = retire;    }    private int age;    private long moneyL;    private float moneyF;    private double moneyD;    private char sex;    private boolean retire;}

 

输出:

 

java基本类型作为成员变量时的初始值