• Java入门 实用类(一)(第二十四天)


    枚举

    枚举指由一组固定的常量组成的类型
       类型安全、易于输入、代码清晰
    (列举出某一个属性所需要的正确值,限制赋值范围)

    1. package GatherDemo06;
    2. //枚举方法:Genders关键字
    3. public enum Genders {
    4. 男,女,东方不败;
    5. }
    6. --------------------------------------------------------------
    7. package GatherDemo06;
    8. public class Student {
    9. public String name;
    10. public Genders gender;
    11. public static void main(String[] args) {
    12. Student student = new Student();
    13. student.name = "张三";
    14. //性别枚举
    15. student.gender = Genders.男;
    16. }
    17. }

    包装类

    包装类把基本类型数据转换为对象
           1)每个基本类型在java.lang包中都有一个 
                相应的包装类

    包装类的作用
           1)提供了一系列实用的方法
           2)集合不允许存放基本数据类型数据,存 
                 放数字时,要用包装类型

    包装类的构造方法

    1)所有包装类都可将与之对应的基本数据类型作为参数,来构造它们的实例(对象)

    2) 除Character类外,其他包装类可将一个字符串作为参数构造它们的实例Number类型包装类使用字符串作为参数构造实例的时候,字符串需要是能够转换为数字的字符串否则就会报NumberFormatException(数字格式化异常)

          public Type(type value)
    如:Integer i=new Integer(1);

         public Type(String value)
    如:Integer i=new Integer("123");

    注意事项

    1)Boolean类构造方法参数为String类型时,若该字符串内容为true(不考虑大小写),则该Boolean对象表示true,否则表示false

    2)当Number包装类构造方法参数为String 类型时,字符串不能为null,且该字符串必须可解析为相应的基本数据类型的数据,否则编译不通过,运行时会抛出NumberFormatException异常

    包装类的常用方法

    语法:

    XXXValue():包装类转换成基本类型
    如:
       Integer integerId=new Integer(25);
       int intId=integerId.intValue();

    toString()方法:以字符串形式返回包装对象表示的基本类型数据(基本类型->字符串)

    String sex=Character.toString('男');
    String id=Integer.toString(25);

    String sex='男'+"";
    String id=25+"";

    parseXXX()方法:把字符串转换为相应的基本数据类型数据(Character除外)(字符串->基本类型)

    int num=Integer.parseInt("36");
    boolean bool=Boolean.parseBoolean("false");

    public static  type parseType(String type)

    valueOf()方法

    1)所有包装类都有如下方法(基本类型->包装类)
        public static Type valueOf(type value)
    如:  Integer intValue = Integer.valueOf(21);

    2)除Character类外,其他包装类都有如下方法(字符串->包装类)
         public static Type valueOf(String s)
    如: Integer intValue = Integer.valueOf("21");

    包装类代码:

    1. package TypeConversion.Demo01;
    2. public class WrapClassDemo01 {
    3. public static void main(String[] args) {
    4. //包装类常用方法: XXXValue():包装类转换成基本类型
    5. //包装类
    6. /*byte by = 12;
    7. Byte byte1 = new Byte(by);*/
    8. //包装类转换成基本类型(byte)
    9. Byte byte1=new Byte("12");
    10. byte num1 = byte1.byteValue();
    11. System.out.println(num1);
    12. //包装类转换成基本类型(boolean)
    13. Boolean bool = new Boolean("True");
    14. boolean num2 = bool.booleanValue();
    15. System.out.println(num2);
    16. //包装类转换成基本类型(Char)
    17. Character ch = new Character('a');
    18. char num3 = ch.charValue();
    19. System.out.println(num3);
    20. /*toString():以字符串形式返回包装对象表示的基
    21. 本类型数据(基本类型- >字符串)*/
    22. byte num4 = 10;//byte基本类型
    23. String str1 = Byte.toString(num4);
    24. System.out.println(str1);
    25. String str2=Boolean.toString(false);//布尔型基本类型
    26. System.out.println(str2);
    27. //parseXXX():把字符串转换为相应的基本数据类型数据(CharactJer除外) (字符串->基 本类型)
    28. int num5 = Integer.parseInt("30");
    29. System.out.println(num5);
    30. boolean num6 = Boolean.parseBoolean("qwe");//值为:false
    31. System.out.println(num6);
    32. //value0f():所有包装类都有如下方法(基本类型->包装类)public static Type value0f(type value)
    33. Integer int1=Integer.valueOf(100);
    34. System.out.println(int1);
    35. //value0f( ) :除Character类外, 其他包装类都有如下方法(字符串->包装类)public static Type value0f(String s)
    36. Long long1=Long.valueOf("200");
    37. System.out.println(long1);
    38. }
    39. }
    1. package TypeConversion.Demo01;
    2. public class WrapClassDemo01 {
    3. public static void main(String[] args) {
    4. //包装类常用方法: XXXValue():包装类转换成基本类型
    5. //包装类
    6. /*byte by = 12;
    7. Byte byte1 = new Byte(by);*/
    8. //包装类转换成基本类型(byte)
    9. Byte byte1=new Byte("12");
    10. byte num1 = byte1.byteValue();
    11. System.out.println(num1);
    12. //包装类转换成基本类型(boolean)
    13. Boolean bool = new Boolean("True");
    14. boolean num2 = bool.booleanValue();
    15. System.out.println(num2);
    16. //包装类转换成基本类型(Char)
    17. Character ch = new Character('a');
    18. char num3 = ch.charValue();
    19. System.out.println(num3);
    20. /*toString():以字符串形式返回包装对象表示的基
    21. 本类型数据(基本类型- >字符串)*/
    22. byte num4 = 10;//byte基本类型
    23. String str1 = Byte.toString(num4);
    24. System.out.println(str1);
    25. String str2=Boolean.toString(false);//布尔型基本类型
    26. System.out.println(str2);
    27. //parseXXX():把字符串转换为相应的基本数据类型数据(CharactJer除外) (字符串->基 本类型)
    28. int num5 = Integer.parseInt("30");
    29. System.out.println(num5);
    30. boolean num6 = Boolean.parseBoolean("qwe");//值为:false
    31. System.out.println(num6);
    32. //value0f():所有包装类都有如下方法(基本类型->包装类)public static Type value0f(type value)
    33. Integer int1=Integer.valueOf(100);
    34. System.out.println(int1);
    35. //value0f( ) :除Character类外, 其他包装类都有如下方法(字符串->包装类)public static Type value0f(String s)
    36. Long long1=Long.valueOf("200");
    37. System.out.println(long1);
    38. }
    39. }

    装箱和拆箱

    基本类型和包装类的自动转换
    Integer intObject = 5;
    int intValue = intObject;

    装箱:基本类型转换为包装类的对象  
    拆箱:包装类对象转换为基本类型的值

    拆箱、装箱代码:

    1. package TypeConversion.Demo01;
    2. public class BoxingUnboxingDemo02 {
    3. public static void main(String[] args) {
    4. /*
    5. *拆箱和装箱:实现的是基本数据类型和包装类类型之间的自动转换
    6. */
    7. byte num1 = 10;
    8. // Byte byte1 = new Byte(num1);
    9. //装箱:基本数据类型直接赋值给基本类型的
    10. Byte byte1 = null;
    11. //拆箱:包装类类型数据直接赋值给基本类型的变量
    12. Integer int1 = new Integer("123");
    13. int num2 = int1;
    14. //JDK1.5后,允许基本数据类型和包装类型进行混合数学运算
    15. Integer int2 = new Integer("223");
    16. int num3 = 300;
    17. int sum= int2+num3;
    18. Integer int3 = int2+num3;
    19. }
    20. }
  • 相关阅读:
    C# - 常用API
    openstack 遇到的error
    (附源码)ssm学生选课系统 毕业设计 170920
    Python实现定时任务的八种方式
    paddle ocr 训练数字识别模型
    【C++】List模拟实现
    信用风险识别问题(MATLAB)
    Cholesterol-PEG-FITC,Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素供应
    银行互联网类业务基于分布式存储的架构设计与实施运维分享
    信息收集&WAF识别&蜘蛛头
  • 原文地址:https://blog.csdn.net/m0_70516782/article/details/126373657