Java学习
第十一天
基本类型的包装类
把基本类型当做对象来使用
byte – Byte
short – Short
int – Integer
long – Long
float – Float
double – Double
char – Character
boolean – Boolean
1. 数字父类Number
子类:Byte,Short,Integer,Long,Float,Double,BigDecimal,BigInteger
取出基本类型值的方法
byteValue(),shortValue(),intValue(),longValue(),floatValue(),doubleValu()
2. Intger类
创建Integer对象: a= { value:6}
Integer a = new Integer(6);
Integer a = Integer.valueOf(6);
Integer 类中存在256个Integer缓存对象,封装-127到128;如果访问指定范围内的值,会访问缓存对象,如果超出范围,会新建对象。
3. Integer类的方法
字符串解析成int
Integer.parseInt();Byte.parseByte()……
Integer.toBinaryString() 转成二进制字符串
Integer.toOctalString() 转成八进制字符串
Integer.toHexString(255) 转成十六进制字符串
4. BigDcimal和BigInteger 类
BigDcimal精确的浮点数运算
BigInteger 超大的整数运算
创建对象:
BigDecimal bd = BigDecimal.valueOf(2);
方法:
add(BigDecimal bd)
subtract(BigDecimal bd)
multiply(BigDecimal bd)
divide(BigDecimal bd)
divide(BigDecimal bd,保留位数,舍入方式)
setScale(保留位数,舍入方式)
5. 自动装箱,自动拆箱
基本类型值,自动装箱成包装对象
Integer a = 6; 编译器编译成: Integer a = Integer.valueOf(6);
自动拆箱(自动拆箱要注意null值)
浙公网安备 33010602011771号