包装类
-
java语言是一个面向对象的语言,但是java中的基本数据类型却不是面向对象的,为了能将基本数据类型视为对象进行处理,Java为每个基本数据类型都提供了包装类,如 int 型的 Integer等...
-
包装类没有无参构造方法,所有包装类的对象都是不可变。
-
将基本数据类型转换成对应的包装类称为装箱(boxing),相反的转换为开箱(unboxing)
一、JAVA的8种基本数据类型与包装类
-
数值型
整数类型 byte short int long (包装类:Byte Short Integer Long)
浮点类型 float double (包装类:Float Double)
-
字符型 char x = 'a'; (包装类:Character)
-
布尔型 boolean x = true; (包装类:Boolean)
二、包装类具用的方法
doubleValue(),floatValue(),intValue(),longValue(),shortValue(),byteValue()...
三、包装类的继承关系
四、常用方法
-
Integer
//构造方法
Integer number = new Integer(7); //输入int型
Integer number = new Integer("7"); //输入数字String型
//常用方法
number.byteValue(); //以byte类型返回该Integer值
number.compareTo(Integer anotherInteger); //在数字上比较两个Integer对象,返回int,如果相等返回0,小于返回负,大于返回正
number.equals(Object IntegerObj); //比较此对象与指定的对象是否相等,返回boolean
number.intValue(); //以int型返回此Integer对象
number.shortValue(); //返回short型
number.toString(); //返回一个Integer值的String对象
number.valueOf(String str); //返回str数字字符串的Integer对象
number.parseInt(String str); //返回str数字字符串的int值
//常量
int maxint = Integer.MAX_VALUE; //int类型最大值
int minint = Integer.MIN_VALUE; //int类型最小值
-
Boolean
//构造方法
Boolean b = new Boolean(true);
Boolean bool = new Boolean("ok"); //false
Boolean bool = new Boolean("true"); //true
//常用方法
booleanValue(); //返回Boolean对象的Boolean类型值
equals(Object obj); //判断调用该方法的对象与obj是否相等
parseBoolean(String s); //将字符串解析为boolean值
toString(); //返回该boolean值的String对象
valueOf(String str); //返回str字符串的boolean值
-
Byte
//构造方法
Byte mybyte = 45;
Byte mybyte = new Byte("45");
//常用方法
byteValue(); //返回byte对象的byte值
compareTo(Byte anotherByte); //在数字上比较两个Byte对象
doubleValue();
intValue();
parseByte(String s);
toString();
valueOf(String str);
equals(Object obj);
//常量
MIN_VALUE byte类型最小值
MAX_VALUE byte类型最大值
SIZE 用于以二进制补码形式表示byte值的位数
TYPE 表示基本类型byte的Class实例
-
Character
//构造方法
Character my char = new Character('s');
//常用方法
charvalue();
compareTo();
...
-
Double
//构造方法
Double(double value);
Double(String str);
//常用方法
byteValue();
intValue();
equals();
compareTo(Double d);
...
//常量
MAX_EXPONENT
MIN_EXPONENT
NEGATIVE_INFINITY
POSITIVE_INFINITY
-
抽象类Number
抽象类Number是BigDecimal,BigInteger,Byte,Doucble,Float,Integer,Long,Short类的父类,Number的子类必须提供将表示的数值转换为byte,double,float,int,long,short的方法
//Number所有子类都包含的方法
byteValue();
intValue();
floatValue();
shortValue();
longValue();
doubleValue();
五、BigInteger与BigDecimal
-
BigInteger
BigInteger支持任意精度的整数,在BigInteger类中封装了多种操作,比如加、减、乘、除、绝对值、相反数、最大公约等
BigInteger n = new BigInteger("2"); //将十进制的2转换为BigInteger形式,参数的双引号不能省略,因为参数是以字符串的形式存在的
public static void main(String[] args) {
BigInteger bigInstance = new BigInteger("4"); //实例化一个大数字
System.out.println("加法操作:" + bigInstance.add(new BigInteger("2")));
System.out.println("减法操作:" + bigInstance.subtract(new BigInteger("2")));
System.out.println("乘法操作:" + bigInstance.multiply(new BigInteger("2")));
System.out.println("除法操作:" + bigInstance.divide(new BigInteger("2")));
System.out.println("除以3的商:" + bigInstance.divideAndRemainder(new BigInteger("3"))[0]);
System.out.println("除以3的余数:" + bigInstance.divideAndRemainder(new BigInteger("3"))[1]);
//...
}
-
BigDecimal
BigDecimal类支持任何精度的定点数,可以用它来精确计算货币值
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("4");
System.out.println("加法操作:" + bigDecimal.add(new BigDecimal("2")));
System.out.println("减法操作:" + bigDecimal.subtract(new BigDecimal("2")));
System.out.println("乘法操作:" + bigDecimal.multiply(new BigDecimal("2")));
System.out.println("除法操作,商小数点后保留4位,并对结果进行四舍五入操作:" + bigDecimal.divide(new BigDecimal("3"), 4, BigDecimal.ROUND_HALF_UP)); //1.3333
//...
}