java - day010 - 基本类型包装,自动装箱和拆箱,日期

基本类型的包装类

byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

 

 

 

 

 

 

Number 数字类型包装类的抽象父类

她的子类:

     Byte,Short,Integer ,Long,Float,Double

     BigDecimal,BigInteger

取出被封装的基本类型值

     byteValue();

     shortValue();

      intValue();

      longValue();

      floatValue();

      doubleValue();

 

Integer 

    创建对象;

    new Integer(5);

    Integer.value(5); 

    Integer 实例内部, 有一个Integer 实例的缓存数组,

    缓存着256个 Integer 对象,

    封装的数字范围-128-127

    valueOf()方法,指定范围的值,

    访问存在的缓存对象,指定范围外的值,

     直接新建对象

 

    方法:

         字符串解析成 int 

            Integer.parseInt("225");

            Integer.parseInt("111111",2);二进制

            Integer.parseInt("377",8);8进制

 

         进制转换:

             Integer.toBinaryString(255);转换二进制;

             Integer.toOctalString(255);转换8进制

             Integer.toHexString(255);转换十六进制

 

Double

    创建对象:

           new Double(3.14);

           Double.value(3.14);

           两者没有区别

           

      方法:

            字符串解析

            Double.parseDouble("3.14");  

            Byte.parseByte();

            Short.parseShort();

            Integer.parseInteger();

            Long.parseLong();

            Float.parseFloat();

            Double.parseDouble();

            Boolean.parseBoolean();

           char 没有字符串解析方法, 

     判断浮点数特殊值

           Infinity      Double.IsInfinite(double d);

           nan          Double.isNan(double d);

 

自动装箱,

       Integer a = 5;

       自动编译为:  Integer a = Integer.valueOf(5);

自动拆箱

       int i = a;

       自动编译为 int i = a.intValue();

 

       a = a +  1;

       a = Integer.valueOf(a.intValue()+1);

       自动拆箱要当心 null 

 

 

BigDecimal , BigInteger

      BigDecimal 用来做精确的浮点数运算

      BigInteger 来做超大的整数运算  

      

  BigDecimal

      创建对象:

      BigDecimal.valueOf(2);

      new BigDecimal("2");

      

      方法

      add(BigDecimal);          加

      subtract(BigDecimal);   减

      multiply(BigDecimal);    乘

      divide(BigDecimal);       除,不支持无理数(无限小数 )

     bigDecimal.pow(n);        求 bigDecimal 的 n 次方 

     

      divide(BigDecimal,保留位数,舍入方式);

      setScale(保留位数,舍入方式);  ROUND_HALF_UP(四舍五入)

        保留位数 0: 0位 , 1: 1位 , -1:10位 , -2: 100位

 

      舍入运算,舍入结果被封装成一个新对象

      位数过长可以用 toString();输出

       

BigInteget 超大长整型运算

 

 

Date    java.util.Date

      封装一个毫秒值, 标识一个具体的时间点,  

      标识 距离 1970年1月1号0点 的时间点

 

     创建对象:

     new Date();// 无参构造,  封装系统当前时间的毫秒值

     new Date(9000000000L);// 封装指定的毫秒值

     

    方法

    getTime();

    setTime();

    compareTo(Date d);   //当前日期 与 参数日期 比较大小

    当前对象大,返回正数,否则负数,判断正负即可.0表示相同

 

SimpleDateFormat   java.text.SimpleDateFormat

    日期格式工具

    把 Date 对象格式化成指定的时间格式

    把时间格式字符串解析成 Date 对象

  

   创建对象

     new SimpleDateFormat(格式);

     格式 yyyy-MM-dd HH:mm:ss

      (大写M:月, 小写m:分,H:24小时制,h:12小时制)

     例如 yy-M-d

 

   方法

     format(Date);// Date 对象 格式化成字符串

     parse(String);// 日期格式字符串,解析成Date 对象

 

 

      

 

 

             

 

 

         

 

 

 

 

    

 

转载于:https://www.cnblogs.com/dingzhijie/p/11105803.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值