1. 泛型
package com.test;
/**
* 泛型接口
* 指定类型:
* public class Interface1 implements IntercaceName<String> {
* private String text;
* @Override
* public String getData() {
* return text;
* }
* }
* 不指定类型:
* public class Interface1<T> implements IntercaceName<T> {
* private T data;
* @Override
* public T getData() {
* return data;
* }
*/
public class T {
public static void main(String[] args) {
print(123);
print("TestString");
// 泛型的上界限定 ? extends
Plate<? extends Fruit> p1 = new Plate<Apple>();
// 泛型的下界限定 super extends
Plate<? super Apple> p2 = new Plate<Fruit>();
}
public static <T> void print(T t) {
System.out.println(t);
}
}
interface Fruit{}
class Apple implements Fruit {}
class Plate<T>{
T data;
}
2. Math
package com.test;
public class MathTest {
public static void main(String[] args) {
System.out.println(Math.abs(-100));//100
System.out.println(Math.min(-100,100));//-100
System.out.println(Math.max(-100,100));//100
//四舍五入
System.out.println(Math.round(-100));//-100
//返回小于等于参数的最大整数
System.out.println(Math.floor(-100));//-101
//返回大于等于参数的最大整数
System.out.println(Math.ceil(-100));//-100
}
}
3. java.util.Objects
package com.test;
import java.util.Objects;
public class ObjectTest {
public static void main(String[] args) {
Object o1 = null;
Object o2 = 1223;
// equals 和 Objects.equals区别
//会报错,o1为null无法比较
//System.out.println(o1.equals(o2));
System.out.println(Objects.equals(o1,o2));//false
// 如果传入参数为null就报错,否则返还原值
System.out.println(Objects.requireNonNull(o2));//123
System.out.println(Objects.requireNonNull(o1));//报错
}
}
4. java.util.Arrays
package com.test;
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
int[] arr = {1,3,5,7,8,6,4,2};
System.out.println(arr);
//输出arr
System.out.println(Arrays.toString(arr));//[1, 3, 5, 7, 8, 6, 4, 2]
//排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));//[1, 2, 3, 4, 5, 6, 7, 8]
//根据元素在数组中的位置输出下标
System.out.println(Arrays.binarySearch(arr,4));//3
//数组扩容
arr = Arrays.copyOf(arr,10);
System.out.println(arr.length);//10
}
}
5. java.math.BigDecimal
package com.test;
import java.math.BigDecimal;
public class BigDecimalTest {
public static void main(String[] args) {
System.out.println(0.1+0.2);//0.30000000000000004
BigDecimal b1 = new BigDecimal(0.1);
System.out.println(b1);//0.1000000000000000055511151231257827021181583404541015625
BigDecimal b2 = new BigDecimal(0.2);
System.out.println(b1.add(b2));//0.3000000000000000166533453693773481063544750213623046875
BigDecimal b3 = new BigDecimal("0.1");
BigDecimal b4 = new BigDecimal("0.2");
//add加 substract减 multiply乘 divide除
System.out.println(b3.add(b4));//0.3
}
}
6. java.util.Date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DataTest {
public static void main(String[] args) throws ParseException {
//创建当前时间Date
Date date1 = new Date();
System.out.println(date1);//Fri Jan 28 16:49:41 CST 2022
//以毫秒记录的时间,从1970年1月1日开始
long time1 = date1.getTime();
System.out.println(time1);//1643359813004
//4小时 * 60分钟 * 60秒 * 1000毫秒
long time2 = date1.getTime() - (4*60*60*1000);
System.out.println(time2);//1643345413004
//创建当前时间Date
Date date2 = new Date(time2);
System.out.println(date2);//Fri Jan 28 12:17:46 CST 2022
// DataFormat
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm ss");
//将date对象 转换为字符串yyyy-MM-dd HH:mm ss格式
String text = format.format(new Date());
System.out.println(text);//2022-02-08 08:46 10
//将yyyy-MM-dd HH:mm ss格式转换为data对象
Date date = format.parse("2022-12-12 12:12 12");
System.out.println(date.getTime());//1643345423004
}
}
7. java.util.Calendar
package com.test;
//一般使用get和set方法
import java.util.Calendar;
public class ClanderTest {
public static void main(String[] args) {
Calendar c1 = Calendar.getInstance();
//获取年份
int year = c1.get(Calendar.YEAR);
//获取月份(月份0-11)
int month = c1.get(Calendar.MONTH);
//查看是当年的第几天
int day = c1.get(Calendar.DAY_OF_YEAR);
System.out.println(year);//2022
System.out.println(month);//1
System.out.println(day);//39
//更改当前月份
c1.set(Calendar.MONTH,1);
//查看该月最大多少天
int m = c1.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(m);//28
}
}