1.API-Objects
2.BigDecimal-构造
- 为什么使用BigDecimal?
java中的 float ,double不是绝对精确的, 有精度损失, 金融,证券领域,对精度敏感,使用 double 不能满足要求
BigDecimal 是 不变的任意精确的浮点数!!
System.out.println(0.09 + 0.01);//0.09999999999999999
System.out.println(1.0 - 0.32);//0.6799999999999999
BigDecimal bd = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println(bd.add(bd2));//0.10
BigDecimal能保存比double还大的数
BigDecimal bd3 = new BigDecimal("99999999999999999999999999999999");
小结:
1.BigDecimal是绝对精确的
2.BigDecimal能保存比double大的数
- BigDecimal的构造方法
public BigDecimal(double val)//NO!!! 不要用这个!
public BigDecimal(String val) //ok
static BigDecimal valueOf(double val) //OK
3.BigDecimal-四则运算
扩展:
除法扩展下,除不尽的情况
/*divide(BigDecimal divisor, int scale, RoundingMode roundingMode)*/
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.2");
//BigDecimal bd2 = new BigDecimal("0.3");
// public BigDecimal add(另一个BigDecimal对象) 加法
BigDecimal add = bd1.add(bd2);
System.out.println("和为" + add);
//System.out.println(0.1 + 0.2);
// public BigDecimal subtract (另一个BigDecimal对象) 减法
BigDecimal subtract = bd1.subtract(bd2);
System.out.println("差为" + subtract);
// public BigDecimal multiply (另一个BigDecimal对象) 乘法
BigDecimal multiply = bd1.multiply(bd2);
System.out.println("积为" + multiply);
// public BigDecimal divide (另一个BigDecimal对象) 除法
BigDecimal divide = bd1.divide(bd2);
//如果除不尽,会有问题,要用3个参数的divide方法
//BigDecimal divide = bd1.divide(bd2, 3, RoundingMode.HALF_UP);
System.out.println("商为"+divide);//
4.BigDecimal-特殊方法
BigDecimal小结
1.什么时候用 BigDecimal?
float,double 有精度损失,金融,证券 项目要求不能有精度损失.
2.如何得到BigDecimal对象?
new BigDecimal("0.1")
BigDecimal.valueOf(0.01);
3.add(),subtract(),multiply(), divide(BigDecimal,300, RoundingMode.HALF_UP)
4.保留指定的小数位数
BigDecimal bd = new BigDecimal("9.345478");
int scale = bd.scale(); //返回的是小数点后有几位
System.out.println("小数点后有几位:" + scale);//
//保留指定的小数位数,返回新对象,原对象不变
BigDecimal bd2 = bd.setScale(2, RoundingMode.HALF_UP);
System.out.println(bd2);
5.BigInteger:比 long 还大的整数,进行运算可以用BigInteger类
5.基本数据类型包装类
要点:
1.包装类?
系统为每一种基本类型都提供了对应的引用类型(类类型)
2.包装类有什么好处?
基本类型不能承载属性和方法,类可以有属性和方法
可以在包装类中定义一些常见的方法和属性,用于操作对应的基本类型,例如:"100"----->int
3.基本类型与包装类的对应关系
byte Byte
short Short
int Integer(特别)
long Long
float Float
double Double
char Character(特别)
boolean Boolean
包装类最常见的操作
字符串----->基本类型
"100"------->100
"3.14"------->double
"true"------->boolean
ArrayList<int>
ArrayList<Integer>
6.Integer-获得对象
要点:
如何得到Integer对象?
Integer类:
static Integer valueOf(int i)
static Integer valueOf(String s)
//自动装箱
Integer in = 100;
Integer.parseInt("100");
7.Integer-自动装箱和自动拆箱
注意:在jdk1.5之后,基本数据类型和包装类之间可以自动装箱和拆箱
要点:
装箱?(发快递,打包)
拆箱?(收快递,拆包)
//int---Integer
Integer in = Integer.valueOf(100);//手动装箱
Integer in2 = 200; //自动装箱
//Integer----->int
int i = in.intValue(); //手动拆箱
int i2 = in; //自动拆箱
8.Integer-类型转换
扩展:
double d = Double.parseDouble("3.14");
boolean b = Boolean.parseBoolean("true");
9.递归-概念
要点:
1.递归的概念
方法体内调用方法本身
2.递归格式(试试)
public static void print() {
System.out.println("方法体内调用方法本身");
print();
}
public static void print(int i) {
//递归要有出口
if (i > 100) return;
System.out.println("方法体内调用方法本身:" + i);
i++;
print(i);
}
3.递归的适应场景:
当调用一个方法时,方法的主体是一样的,只是每次调用的参数不一样.这时可以考虑使用递归.(效率很低,是最后的选择)
例如:遍历文件夹,遍历树形菜单
4.递归的分类(了解)
直接递归:方法自己调用自己
间接递归: a调用b,b调用a
public static void a() {
b();
}
public static void b() {
a();
}
5.递归的注意
1.递归要有出口
2.调用次数不能太多
10.递归-求阶乘代码实现
public static void main(String[] args) {
//需求: 求5! ?
System.out.println(jc(5));
}
//1.写一个方法(明确参数和返回值类型)
public static int jc(int n) { //参数:要求哪个数的阶乘 返回值: 返回它的阶乘
// 3.写出口
if (n == 1) {
return 1;
}
//一个数的阶乘 = 本身 * 前一个数的阶乘
//2.找规律
return n * jc(n - 1);
}
要点:
写一个递归的步骤是??
\1. 需求
求5的阶乘?
5! = 5 * 4 * 3 * 2 * 1;
\2. 分析规律
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1
规律: n! = n * (n-1)!
3..写递归的三个步骤
1.写一个方法(明确输入什么,输出什么,方法体做什么) 给一个数,返回它的阶乘 int jc(int num);
2.找规律 本身 * 前一个数的阶乘
3.找出口条件 1的阶乘是1
======================================================================================
递归使用的前提:
当调用一个方法时,方法的主体是一样的,只是每次调用的参数不一样.可以考虑使用递归.
例如:遍历文件夹,遍历树形菜单
11.Arrays
Arrays类包含用于操作数组的各种方法
//返回指定数组的内容的字符串表示形式
public static String toString(int[] a);
//按照数字顺序排列指定的数组
public static void sort(int[] a);
//利用二分查找返回指定元素的索引
public static int binarySearch(int[] a,int key);
工具类设计思想
- 构造方法用 private 修饰
- 成员方法用 public static 修饰