经过了这么长时间的学习,API有很多,这里我在这里列举了很常用的API和引用类型详解!!
下面进入正题
1、BigInteger类
1.1、概述
BigInteger是不可变得任意精度的整数,如果在计算中数据卡类型的范围超过了long,可以使用BigInteger类来计算,该类计算整数是不限制长度的
1.2、构造方法
在计算过程中,计算的长度超过了long的范围,那就不能称之为数字了,就成了对象了,BigInteger(String value) 将 BigInteger 的十进制字符串表示形式转换为 BigInteger,可以将超大型整数封装成对象。
1.3、成员方法
BigInteger为用户提供可用的加、减、乘、除的方法
注意:计算过程中都是与BigInteger进行计算的
方法声明 | 描述 |
---|---|
add(BigInteger value) | 返回其值为 (this + val) 的 BigInteger,超大整数加法运算 |
subtract(BigIntegervalue) | 返回其值为 (this - val) 的 BigInteger,超大整数减法运算 |
multiply(BigInteger value) | 返回其值为 (this * val) 的 BigInteger,超大整数乘法运算 |
divide(BigInteger value) | 返回其值为 (this / val) 的 BigInteger,超大整数除法运算,除不尽取整数部分 |
【示例】
public static void main(String[] args){
BigInteger big1 = new BigInteger("8327432493258329432643728320843");
BigInteger big2 = new BigInteger("98237473274832382943274328834");
//加法运算
BigInteger add = big1.add(big2);
System.out.println("求和:"+add);
//减法运算
BigInteger sub = big1.subtract(big2);
System.out.println("求差:"+sub);
//乘法运算
BigInteger mul = big1.multiply(big2);
System.out.println("乘积:"+mul);
//除法运算
BigInteger div = big1.divide(big2);
System.out.println("除法:"+div);
}
2、BigDecimal类
2.1、引入
前段时间我有个朋友去面试金融行业的开发,因为要牵扯到很精确数字的计算,面试官问在计算小数的运算过程中,用double好还是float好,很多人猜到当然用双精度啦,比较准确,我那个朋友也不例外,回答了用双精度,结果可想而知,被无情拒绝了,很多人不思其解,难道双精度也不是最精确的吗,答案是对的,还有一个家伙,专门计算小数类的,精确无比,没有比他更精确的了,下面来介绍一下这个BigDecimal怎么用吧。
2.2、构造方法
构造 | 描述 |
---|---|
BigDecimal(double val) | 将double类型的数据封装为BigDecimal对象 |
BigDecimal(String val) | 将 BigDecimal 的字符串表示形式转换为 BigDecimal |
注意:推荐使用第二种方式,第一种存在精度问题;
2.3、常用方法
BigDecimal类中使用最多的还是提供的进行四则运算的方法,如下:
方法声明 | 描述 |
---|---|
public BigDecimal add(BigDecimal value) | 加法运算 |
public BigDecimal subtract(BigDecimal value) | 减法运算 |
public BigDecimal multiply(BigDecimal value) | 乘法运算 |
public BigDecimal divide(BigDecimal value) | 除法运算 |
注意:对于divide方法来说,如果除不尽的话,就会出现java.lang.ArithmeticException异常。此时可以使用divide方法的另一个重载方法;
BigDecimal divide(BigDecimal divisor, int scale, int roundingMode): divisor:除数对应的BigDecimal对象; 参数说明:scale:精确的位数;roundingMode取舍模式
举例说明:
package cn.itcast.HomeWork.BigDecima;
import java.math.BigDecimal;
/**
* 有以下double数组:
* double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
* 请编程计算它们的总值及平均值(四舍五入保留小数点后2位)
*/
public class Demo01 {
public static void main(String[] args) {
double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
//接受计算后数组的总和
BigDecimal bigDecimal = new BigDecimal(sum);
System.out.println("总和是:"+bigDecimal);
//计算数组的长度,转成BigDecimal
BigDecimal num = new BigDecimal(arr.length);
BigDecimal mean = bigDecimal.divide(num,2,2) ;
System.out.println("平均值:"+mean);
}
}
小结:Java中小数运算有可能会有精度问题,如果要解决这种精度问题,可以使用BigDecimal
3、Arrays类
3.1、Arrays常用方法
- public static void sort(int[] a):按照数字顺序排列指定的数组
- public static String toString(int[] a):返回指定数组的内容的字符串表示形式
示例代码:
public static void main(String[] args) {
int[] arr = {432, 53, 6, 323, 765, 7, 254, 37, 698, 97, 64, 7};
//将数组排序
Arrays.sort(arr);
//打印数组
System.out.println(Arrays.toString(arr));
}
打印结果:
[6, 7, 7, 37, 53, 64, 97, 254, 323, 432, 698, 765]
4、包装类
4.1、概述
Java提供了两个类型系统,基本类型与引用类型,使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类,如下:
基本类型 | 对应的包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
4.2、Integer类
- Integer类概述
包装一个对象中的原始类型 int 的值 - Integer类构造方法及静态方法
方法名 | 说明 |
---|---|
public Integer(int value) | 根据 int 值创建 Integer 对象(过时) |
public Integer(String s) | 根据 String 值创建 Integer 对象(过时) |
public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
public static Integer valueOf(String s) | 返回保存指定String值的 Integer 对象 |
示例代码:
public class IntegerDemo {
public static void main(String[] args) {
//public Integer(int value):根据 int 值创建 Integer 对象(过时)
Integer i1 = new Integer(100);
System.out.println(i1);
//public Integer(String s):根据 String 值创建 Integer 对象(过时)
Integer i2 = new Integer("100");
//Integer i2 = new Integer("abc"); //NumberFormatException
System.out.println(i2);
System.out.println("--------");
//public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
Integer i3 = Integer.valueOf(100);
System.out.println(i3);
//public static Integer valueOf(String s):返回保存指定String值的Integer对象
Integer i4 = Integer.valueOf("100");
System.out.println(i4);
}
}
4.3 装箱与拆箱
基本类型与对应的包装类对象之间,来回转换的过程称为”装箱“与”拆箱“:
- 装箱:从基本类型转换为对应的包装类对象。
- 拆箱:从包装类对象转换为对应的基本类型。
基本数值---->包装对象:
Integer i = new Integer(4);//使用构造函数函数
Integer iii = Integer.valueOf(4);//使用包装类中的valueOf方法
包装对象---->基本数值:
int num = i.intValue();
4.4 自动装箱与自动拆箱
由于我们经常要做基本类型与包装类之间的转换,从Java 5(JDK 1.5)开始,基本类型与包装类的装箱、拆箱动作可以自动完成。例如:
Integer i = 4;//自动装箱。相当于Integer i = Integer.valueOf(4);
i = i + 5;//等号右边:将i对象转成基本数值(自动拆箱) i.intValue() + 5;
//加法运算完成后,再次装箱,把基本数值转成对象。
4.5 基本类型与字符串之间的转换
基本类型转换为String
方式一:直接在数字后加一个空字符串
方式二:通过String类静态方法valueOf()
示例代码:
public class IntegerDemo {
public static void main(String[] args) {
//int --- String
int number = 100;
//方式1
String s1 = number + "";
System.out.println(s1);
//方式2
//public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2); System.out.println("--------");
}
}
String转换成基本类型
- public static byte parseByte(String s) :将字符串参数转换为对应的byte基本类型。
- public static short parseShort(String s) :将字符串参数转换为对应的short基本类型。
- public static int parseInt(String s) :将字符串参数转换为对应的int基本类型。
- public static long parseLong(String s) :将字符串参数转换为对应的long基本类型。
- public static float parseFloat(String s) :将字符串参数转换为对应的float基本类型。
- public static double parseDouble(String s) :将字符串参数转换为对应的double基本类型。
- public static boolean parseBoolean(String s) :将字符串参数转换为对应的boolean基本类型。
代码使用(仅以Integer类的静态方法parseXxx为例)如:
转换方式
- 方式一:先将字符串数字转成Integer,再调用valueOf()方法
- 方式二:通过Integer静态方法parseInt()进行转换
示例代码:
public class IntegerDemo {
public static void main(String[] args) {
//String --- int
String s = "100";
//方式1:String --- Integer --- int
Integer i = Integer.valueOf(s);
//public int intValue()
int x = i.intValue();
System.out.println(x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
注意:如果字符串参数的内容无法正确转换为对应的基本类型,则会抛出
java.lang.NumberFormatException 异常。
下节会更新String的用法详解,敬请期待