常见类的使用(2)

1、自动拆箱装箱

        由于基本数据类型不具备对象的特征,为了解决此需求java语言引入了包装类。        

基本数据类型对应的包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

        每个包装类可以封装其对应的基本数据类型,并使其具备了对象的特征。

        1.1、Integer

        构造器:Integer  x  =  new  Integer (100);         Integer  x  = new Integer("100");

        最大值:Integer.MAX_VALUE

        最小值:Integer.MIN_VALUE

        字符串转Integer(其他类型通用):

                public static int parseInt(String s)将字符串转为整数

                public static Integer valueOf(String s)将字符串转为Integer类型

                public static Integer deCode(String s)将字符串解码为Integer类型

        转换成其他进制:

                Integer.toBinaryString(int n),转为二进制

                Integer.toOctalString(int n),转换为八进制

                Integer.toHexString(int n),转换为十六进制

        比较和计算:

                Integer x=15;Intger y=10;                比较x和y的差值:x.compareTo(y)       5

                Integer.max(x,y)最大值        Integer.min(x,y)最小值       Integer.sum(x,y)求和

        *字符串转基本数据类型,valueOf

        *包装类和基本数据类型可以自动转换

        *包装类比基本数据类型多一个null

2、日期操作的类

        2.1、Date类

                格林威治时间:1970年1月1日00:00:00;

        Date的构造方法:

                public Date(){}获得当前时间。

                public Date(long date){}获得指定毫秒的时间。

        常用方法:       

                gettime()获取当前时间的毫秒值。

                public Boolean after(Date when)测试此日期是否在指定日期之后。

                public Boolean before(Date when)测试日期是否在指定日期之前。

        2.2、SimpleDateFormat类(时间格式化)

                Date对象表示的默认时间是星期、月、日、小时、秒、年

                构造方法:SimpleDateFormat(String pattern)    pattern为指定格式如:yyyy-MM-dd

                常用方法:

                        public Date parse(String source)解析字符串,生成日期

                        public String format(Date date)讲一个date格式化为字符串

                可以通过System的静态方法,public long currentTimeMillis()获取当前的毫秒数。

3、BigInteger和BigDecimal

        大整数和大浮点数。理论上无限大。

        3.1、BigInteger大整数

        构造方法:

                public BigInteger(String val)将十进制字符串转换为大整数

        成员方法:

                public BigInteger add(BigInteger val)加法

                public BigInteger subtract(BigInteger val)减法

                public BigInteger multiply(BigInteger val)乘法

                public BigInteger divide(BigInteger val)除法

                public BigInteger remaider(BigInteger val)求余

                public int intValue()转int类型

        3.2、Bigdecimal

                与BigInteger基本相似。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值