一、基本类型的包装类
1.概念
将8种基本类型进行封装,形成对象,变成引用变量
2.对应关系如下
3.用法
1)xxx.parse(String str); //将满足格式的字符串转换成相应的封装基本变量类型
Integer.parseInt("123") + 2
System.out.println(Integer.parseInt("123") + 2);
//打印结果为 125
2)xxx.toString(); //将基本数值转成字符串
Integer.toString(34)
;
4.基本类型和封装类型的相互转换
1)基本数值---->包装对象
Integer i = new Integer(4);//使用构造函数函数
Integer ii = new Integer("4");//构造函数中可以传递一个数字字符串
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法
Integer iiii = Integer.valueOf("4");//使用包装类中的valueOf方法
2)包装对象---->基本数值
int num = i.intValue();
5.注意
//当数值在byte范围之内时,进行自动装箱,不会新创建对象空间而是使用医来已有的空间。
Integer a = new Integer(3);
Integer b = new Integer(3);
System.out.println(a==b);//false
System.out.println(a.equals(b));//true
System.out.println("---------------------");
Integer x = 127;
Integer y = 127;
//在jdk1.5自动装箱时,如果数值在byte范围之内,不会新创建对象空间而是使用原来已有的空间。
System.out.println(x==y); //true
System.out.println(x.equals(y)); //true
二、System类
1.常用方法
l --currentTimeMillis() 获取当前系统时间与1970年01月01日00:00点之间的毫秒差值
l --exit(int status) 用来结束正在运行的Java程序。参数传入一个数字即可。通常传入0记为正常状态,其他为异常状态
l --gc() 用来运行JVM中的垃圾回收器,完成内存中垃圾的清除。
l --getProperty(String key) 用来获取指定
键(字符串名称)中所记录的系统属性信息
|--arrayCopy(srcArr,startIndex,resArr,startIndex,count) 用来拷贝数组,将src的数组从start开始,拷贝到res的start数组中,拷贝count个
三、
Math类
1.概念
Math是一个工具类,数学工具,比如加减乘除,平方,三角函数等
2.常用方法
|--abs方法,结果都为正数
double d1 = Math.abs(-5); // d1的值为5
double d2 = Math.abs(5); // d2的值为5
l-- ceil方法,结果为比参数值大的最小整数的double值
double d1 = Math.ceil(3.3); //d1的值为 4.0
double d2 = Math.ceil(-3.3); //d2的值为 -3.0
double d3 = Math.ceil(5.1); // d3的值为 6.0
l-- floor方法,结果为比参数值小的最大整数的double值
double d1 = Math.floor(3.3); //d1的值为3.0
double d2 = Math.floor(-3.3); //d2的值为-4.0
double d3 = Math.floor(5.1); //d3的值为 5.0
l --max方法,返回两个参数值中较大的值
double d1 = Math.max(3.3, 5.5); //d1的值为5.5
double d2 = Math.max(-3.3, -5.5); //d2的值为-3.3
l --min方法,返回两个参数值中较小的值
double d1 = Math.min(3.3, 5.5); //d1的值为3.3
double d2 = Math.max(-3.3, -5.5); //d2的值为-5.5
l --pow方法,返回第一个参数的第二个参数次幂的值
double d1 = Math.pow(2.0, 3.0); //d1的值为 8.0
double d2 = Math.pow(3.0, 3.0); //d2的值为27.0
l-- round方法,返回参数值四舍五入的结果
double d1 = Math.round(5.5); //d1的值为6.0
double d2 = Math.round(5.4); //d2的值为5.0
l --random方法,产生一个大于等于0.0且小于1.0的double小数
double d1 = Math.random();
四、Arrays类
1.概念
此类事相当于是数组的工具类,针对数组进行操作,比如排序和搜索等
2.常用方法
1)sort方法,用来对指定数组中的元素进行排序(元素值从小到大进行排序)
//源arr数组元素{1,5,9,3,7}, 进行排序后arr数组元素为{1,3,5,7,9}
int[] arr = {1,5,9,3,7};
Arrays.sort( arr );
2)toString方法,用来返回指定数组元素内容的字符串形式
int[] arr = {1,5,9,3,7};
String str = Arrays.toString(arr); // str的值为[1, 3, 5, 7, 9]
3)binarySearch方法,在指定数组中,查找给定元素值出现的位置。若没有查询到,返回位置为-1。要求该数组必须是个有序的数组。
int[] arr = {1,3,4,5,6};
int index = Arrays.binarySearch(arr, 4); //index的值为2
int index2= Arrasy.binarySearch(arr, 2); //index2的值为-1
五、BigInteger类,大数据类
1.概念
java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符.
2.四则运算代码
/
public static void main(String[] args) {
//大数据封装为BigInteger对象
BigInteger big1 = new BigInteger("12345678909876543210");
BigInteger big2 = new BigInteger("98765432101234567890");
//add实现加法运算
BigInteger bigAdd = big1.add(big2);
//subtract实现减法运算
BigInteger bigSub = big1.subtract(big2);
//multiply实现乘法运算
BigInteger bigMul = big1.multiply(big2);
//divide实现除法运算
BigInteger bigDiv = big2.divide(big1);
}
六、BigDecimal类,大数据类
1.概念
double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算
2.四则运算代码
public static void main(String[] args) {
//大数据封装为BigDecimal对象
BigDecimal big1 = new BigDecimal("0.09");
BigDecimal big2 = new BigDecimal("0.01");
//add实现加法运算
BigDecimal bigAdd = big1.add(big2);
BigDecimal big3 = new BigDecimal("1.0");
BigDecimal big4 = new BigDecimal("0.32");
//subtract实现减法运算
BigDecimal bigSub = big3.subtract(big4);
BigDecimal big5 = new BigDecimal("1.105");
BigDecimal big6 = new BigDecimal("100");
//multiply实现乘法运算
BigDecimal bigMul = big5.multiply(big6);
3.取舍
对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式