014_常用类
Object
- 他是所有类的超类(父类),而且他是默认被所有子类继承。
- getclass 返回实际对象的类型
- toString 返回的是该对象地址的文本格式(根据需要选择是否重写)
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
3.hashCode() 返回地址的10进制;相同对象的哈希值一定相同,不同对象的哈希值可能相同
4.equals(Object == obj) this ==obj 判断的是地址。根据需求选择是否重写,目的是为了把相同的内容返回true
包装类
基本数据类型对应的引用数据类型
byte Byte short Short int Integer long Long float Float double Double boolean Boolean char Character
Integer
装箱:把基本数据类型转为引用数据类型
// 手动 // 方式一 Integer i = new Integer(1); //方式二 Integer i = Integer.valueOf(1); // 自动 Integer i = 1;// 底层是调用的valueOf
拆箱:与装箱相反
// 手动 int j = Integer.intvalue(i); // 自动 int j = i;// 底层调用的是手动版
转整数
Integer.parseInt(str)
// 特别注意 如果字符串里有非数字格式 会报异常:NumberFormatException
// Integer 在做运算的时候会自动拆箱
String
字符串String 用final 修饰,不可被继承 不可变化
底层就是char[] value
String常用方法
1.equals 根据需求重写,内容相同即为true 2.charAt(index) 通过下标查找字符 3.contains true/false 判断是否包含 4.toCharArray() 字符串转成 字符数组 5.length() 获取字符串长度 6.indexOf(str) 通过字符找到它首次出现的下标,如果没有这个字符就返回-1 7.lastindexOf(str) 最后一次出现的下标 8.subString(int begin) 从开头截取到最后 9.subString(int begin,int end) [begin end) 10.trim() 去掉前面的空格 11.toUpperCase() 转大写 toLowerCase() 转小写 12.startWith(str) 以str开始 endWith(str) 以str结束 13.replace(old,new) 返回一个新的字符串 14.split(分隔符(字母,数字等)) 切割成数组 15.new String(char[] c) 数组转字符串
16.concat() 拼接
StringBuffer与StringBuilder
StringBuffer与StringBuilder 都属于可变字符串
底层原理:有一个初始长度为16的字符数组。每次添加字符串的时候都是添加到该数组中,数组长度不够存储的时,会创建一个容量大的数组,通过Arraycopyof把旧数组复制到新数组中,实现扩容。
常用的方法:
增加 append
删除 delete
插入 insert
修改 setCharAt
查询 charAt
StringBuffer 线程安全效率比StringBuilder慢一些;
StringBuilder 线程不安全,但是效率比较快
Bigdecimal
用于精确计算浮点数
BigDecimal b1 = new BigDecimal("1.0");
BigDecimal b2 = new BigDecimal("0.9");
//相减
System.out.println(b1.subtract(b2));
// 相加
System.out.println(b1.add(b2));
// 相乘
System.out.println(b1.milpitly(b2));
// 相除
System.out.println(b1.divide(b2,4,BigDecimal.ROUND_HALF_UP));
// 相除时,如果除不尽会报算数异常错误,由此引入了四舍五入保留小数点的方式
Math
Math.util工具类下的方法
// 取绝对值 int a = Math.abs(-1); // 取大值或小值 Math.max(1,2);//2 Math.min(1,2);//1 // 四舍五入 Math.round(3.6);//4 Math.round(3.4);//3 // 向上取整 返回值是double类型 Math.ceil(3.1);//4.0 // 向下取整 Math.floor(3.99);//3.0 // 随机数 是double 类型 Math.random();//[0 1) // 0-50之间的整数 Math.random()*51;//[0 51) (int)Math.floor(Math.random()*51); // 平方根 Math.sqrt(100);//2 //求幂 double pow = Math.pow(2,3);// 8.0
System
1.error //错误输出流
2.out //输出流
3.in //输入流
常用方法:
1. 复制数组
int[] arr = {11,22,33};
int[] newArr = new int[arr.length];
// 方式一
arrays.copyOf(arr.arr.length);// 底层调用的System.arraycopy
//方式二
System.arraycopy(arr,0,newArr,0,arr.length);
// 打印1970到现在的毫秒数 1s = 1000ms
long start = System.currentTimeMillis();
// exit(0);退出虚拟机
System.exit(0);
//gc() 垃圾回收
Date
Date date = new Date();// 导包
System.out.println(date.toString());
System.out.println(date.toLocaleString());
//getDate() 日
System.out.println(date.getDate());
//getMonth() 月
System.out.println(date.getMonth());//[0 11]
//getYear()年
System.out.println(date.getYear());
SimpleDateFormat
把date日期对象转换成自定义格式
//定义的日期输出模板"yyyy/MM/dd hh:mm:ss"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date();
//把日期对象转成 字符串(符合模板的)
String dateStr = sdf.format(date);
System.out.println(dateStr); //2019年12月09日 19:45:37
注意:
y 年
M 月
d 日
H 时
m 分
s 秒
// 2.把字符串转日期
String str = "2000年12月19日 12点15分30";
Date date2 = sdf.parse(str);
System.out.println(date2);

浙公网安备 33010602011771号