包装类&数学类
包装类&数学类
1 包装类
| byte | short | int | long | float | double | char | boolean | void |
|---|---|---|---|---|---|---|---|---|
| Byte | Short | Integer | Long | Float | Double | Character | Boolean | Void |
Void构造方法是私有的,并且是一个最终类,没有子类
public static Void m(){
return null;
}
数值类型
Byte Short Integer Long Float Double 都有一个共同的父类 Number
自动装箱:
自动装箱底层都会调用valueOf()方法
Byte、Short、Integer和Long类型,在-128到127范围内,返回的都是同一个对象
Float和Double永远返回新的对象
Character在0-127范围内返回的是同一个对象
Boolean就两个值:TRUE 和FALSE
包装类产生的对象的哈希码值是固定不变的
Byte、Short、Integer返回的就是数值本身
Character返回的是字符的码表值
Boolean如果是true返回1231,如果是false返回1237
public class TestDemo2 {
public static void main(String[] args) {
// 装箱:把基本数据类型包装成了引用数据类型
Integer i = new Integer(10);
System.out.println(i);
// JDK5特性:自动装箱:自动把基本数据类型包装成了引用数据类型
// 自动装箱底层调用了valueOf()方法
Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2);
// 拆箱:把引用数据类型转换成基本数据类型
int i1 = integer1.intValue();
// JDK5特性:自动拆箱 自动把引用数据类型转换成基本数据类型
// 底层调用的就是xxxValue方法
int a = integer2;
Double d = 3.14;
double d1 = d;
// 当引用数据类型和基本数据类型进行运算时,会自动拆箱
if (integer1 > 100){
System.out.println("integer1大于100");
}
// Integer integer = new Integer("123");
// System.out.println(integer);
//
// Double v = new Double("3.14");
// System.out.println(v);
// 把字符串转换成基本数据类型
int parseInt = Integer.parseInt("123");
System.out.println(parseInt);
// Double.parseDouble()
boolean f = Boolean.parseBoolean("tRue");
System.out.println(f);
// 字符没有这个方法
// 包装类产生的对象的哈希码值是固定不变的
Byte b = 10;
System.out.println(b.hashCode());
double y = 0.0 / 0;
// NaN和自身都不相等
// System.out.println(y == (0.0 / 0));
// 判断是否是NaN
System.out.println(Double.isNaN(0.0 / 0));
// 判断是否是正无穷
System.out.println(Double.isInfinite(1.0 / 0));
// 转换成二进制
System.out.println(Integer.toBinaryString(5));
// 转换成八进制
System.out.println(Integer.toOctalString(10));
// 转换成十六进制
System.out.println(Integer.toHexString(11));
}
public static Void m(){
return null;
}
}
2 数学类
2.1 Math类
public static void main(String[] args) {
// 自然指数
System.out.println(Math.E);
// 圆周率
System.out.println(Math.PI);
// 绝对值
System.out.println(Math.abs(-10));
// 立方根
System.out.println(Math.cbrt(27));
// 向上取整
System.out.println(Math.ceil(3.1));
// 向下取整
System.out.println(Math.floor(3.9));
// 四舍五入
System.out.println(Math.round(3.5));
// 最大值
System.out.println(Math.max(10,20));
// 最小值
System.out.println(Math.min(10,20));
// 加权函数
System.out.println(Math.pow(10,3));
// 随机数 [0,1)
System.out.println(Math.random());
// 平方根
System.out.println(Math.sqrt(9));
}
2.2 BigDecimal类
public static void main(String[] args) {
// double d1 = 3.0;
// double d2 = 2.99;
// System.out.println(d1 - d2);
// 用于精确计算的类,要求参数以字符串形式传递,底层用的是按位运算
BigDecimal bigDecimal1 = new BigDecimal("10");
BigDecimal bigDecimal2 = new BigDecimal("3");
// 加法
System.out.println(bigDecimal1.add(bigDecimal2));
// 减法
System.out.println(bigDecimal1.subtract(bigDecimal2));
// 乘法
System.out.println(bigDecimal1.multiply(bigDecimal2));
// 除法
System.out.println(bigDecimal1.divide(bigDecimal2, RoundingMode.DOWN));
}
2.3 BigInteger类
用于大量数字的存储和计算,最多可以放67,108,864位
public static void main(String[] args) {
BigInteger bigInteger1 = new BigInteger("48574385742385974895743");
BigInteger bigInteger2 = new BigInteger("485743857423859748957432895");
// 乘法
System.out.println(bigInteger1.multiply(bigInteger2));
}
2.4 DecimalFormat类
public static void main(String[] args) {
double d = 1254656.35567656546546;
System.out.println(d);
// 用0来表示占位,表示一位数字,如果没有数字,用0代替
DecimalFormat df = new DecimalFormat("00.00");
System.out.println(df.format(d));
// #代表占位,表示一位数字。如果这一位没有数字,那么就不填充
DecimalFormat df1 = new DecimalFormat("#0.0#");
System.out.println(df1.format(d));
// 科学计数法
DecimalFormat df2 = new DecimalFormat("0.000000E0");
System.out.println(df2.format(d));
}
面试题:两个千位数字相乘,有几种方案?
第一种方案:BigInteger
第二种方案:存储和相乘
存储可以用int类型的数组
public static void main(String[] args) {
int[] arr1 = {1,2,3,4};
int[] arr2 = {5,6,7,8};
int[] result = new int[8];
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
result[i+j] += arr1[i] * arr2[j];
}
}
for (int i = 0; i < result.length - 1; i++) {
int temp = result[i];
result[i] = temp % 10;
result[i+1] += (temp / 10);
}
//结果反转
for (int i = 0;i < result.length / 2;i++){
int temp = result[i];
result[i] = result[result.length - 1 - i];
result[result.length - 1 - i] = temp;
}
System.out.println(Arrays.toString(result));
}

浙公网安备 33010602011771号