天天看點

java常見API1.枚舉2.包裝類

1.枚舉

1.1使用非枚舉的方式對Student類的sex屬性指派

public class Student {

    private String sex;

    public String getSex() {

        return sex;

    }

    public void setSex(String sex) {

        this.sex = sex;

    }

    public static void main(String[] args) {

        Student student = new Student();

        student.setSex("女");

        System.out.println(student.getSex());

        student.setSex("asd");

        System.out.println(student.getSex());

    }

}

1.2使用枚舉的方式對Student類中的sex屬性指派

//定義枚舉類型,裡面的内容為限制的各個指派

public enum Gender {

    男,女

}

//指派

public class Student {

    private Gender sex;  // 定義sex屬性的類型為枚舉類型

    public Gender getSex() {  // get,set方法

        return sex;

    }

    public void setSex(Gender sex) {

        this.sex = sex;

    }

    public static void main(String[] args) {

        Student student = new Student();

        student.setSex(Gender.女);

        System.out.println(student.getSex());  // 指派方法改變

//        student.setSex("asd");   // 一般指派的方法會報錯

//定義枚舉類型,裡面的内容為限制的各個指派

public enum Gender {

男,女

}

關鍵字:enum 替換 class

2.包裝類

2.1定義

每個基本類型在Java.lang包中都有一個對應的包裝類

java常見API1.枚舉2.包裝類

2.2作用

1)提供了一系列的實用方法

2)在集合中不能存放基本資料類型,有時候為了使用基本資料作為對象類型,就需要添加包裝類

int,short,等是基本資料類型,不能算對象類型,是以經過包裝類,可以在泛型中使用

ArrayList不允許

ArrayList可以,Integer為int的包裝類

//成員變量為基本資料類型時,系統會預設指派為0,但是包裝類不會預設指派,和對象類型,String類型一樣,不指派的情況下都是null

//基本資料類型和包裝資料類型的差別:

public class Demo2 {

    private int a;

    private Integer b;

    private Demo2 d;

    public static void main(String[] args) {

        Demo2 demo2 = new Demo2();

        System.out.println(demo2.a);  // 0

        System.out.println(demo2.b);  // null

        System.out.println(demo2.d);  // null

    }

}

2.3構造方法(基本資料類型轉換成包裝類)

2.3.1兩種基本方法

  1. public class Demo3 {

        public static void main(String[] args) {

            int a = 1;

            Integer a_new = new Integer(a);

            byte b = 2;

            Byte b_new = new Byte(b);

            short c = 3;

            Short c_new = new Short(c);

            long d = 4;

            Long d_new = new Long(d);

            float e = 5;

            Float e_new = new Float(e);

            double f = 6;

            Double f_new = new Double(f);

            char g = 'a';

            Character g_new = new Character(g);

            boolean i = true;

            Boolean i_new = i;

        }

    }

  2. 2.3.2構造方法,算數,拼接

  3. public class Demo4 {

        public static void main(String[] args) {

            //兩個字元串拼接

            String a = "123";

            System.out.println(a + "1");  // 1231

            //一個字元串,一個基本資料類型進行 "+",拼接

            String b = "123";

            System.out.println(b + 1);  // 1231

            int c = 123;

            String d = c + "";

            System.out.println(d);  // 123

            //兩個數值類型的相加

            System.out.println(123 + 1);  // 124

            //将字元串包裝後和數值進行"+"操作--->算數操作

            String e  = "123";

            Integer e_new = new Integer(e);

            System.out.println(e_new + 1);  // 124

        }

    }

  4. 2.3.3兩個構造中的常見特例:

  5. public class Demo5 {

        public static void main(String[] args) {

            // 會報錯,Character不可以用字元串來構造

    //        Character a = new Character("abcd");

            // Boolean 類型的除了(true,不分大小寫)以外,都是false

            Boolean b = new Boolean("sfa");

            Boolean b1 = new Boolean("trUe");

            System.out.println(b); // false

            System.out.println(b1); // true

        }

    }

  6. 2.4常見方法

    2.4.1 intValue() 包裝類轉換成基本資料類型

  7. //将包裝類轉換成基本類型

    public class Demo6 {

        public static void main(String[] args) {

            Integer a = new Integer(123);

            a.intValue();

            Double b = new Double(20.0);

            b.doubleValue();

        }

    }

2.4.2 toString() 基本類型轉換成字元串類型 

 public class Demo7 {

    public static void main(String[] args) {

        // 第一種:常用轉字元串,拼接

        int a = 12;

        String a_new = a + "";

        // 第二種toString()

        int b = 12;

        String b_new = Integer.toString(b);

        double c = 12.2;

        String c_new = Double.toString(c);

    }

}

2.4.3 parseXXX() 字元串類型--->基本類型(Character除外)

2.4.4 valueOf()