记录一下Java比较核心的以及一些常用的类
1.java.util.Objects类
所属包 java.utils
1.1 equals(Object a,Object b))
方法定义public static boolean equals(Object a, Object b)
如果参数彼此相等,返回true,其他返回false。 因此,如果这两个参数是null , 返回true,如果只有一个参数为null ,返回false。
举个例子,新建一个Person类,生成他的equals函数
public class Person {
private String name;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name);
}
}
Main类中
import java.util.Objects;
public class Main {
public static void main(String[] args) {
Person p1 = null;
Person p2 = new Person();
System.out.println(Objects.equals(p1,p2));
}
}
此时,调用了Objects.equals()的方法,来比较p1,p2,运行结果如下
那如果将System.out.println(Objects.equals(p1,p2));改为System.out.println(p1.equals(p2));
运行结果呢?
就直接产生了空指针异常,所以如果在程序中想要进行比较的话,最好是使用Object.equals(a,b)进行比较,不容易被抛出异常,有利于程序正常进行。。。
那Object.equals是如何进行判断的呢?接下来让我们一起看下源码
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
直接看return 如果a==b的话那就可以直接返回true了,如果a,b不相等。
就开始运行源码中的 (a != null && a.equals(b)); 如果a 为null的话,也就是&&的左边是false,根据&&的特性,&&只有左边为true的情况下,才会继续运行&&的右边。所以a为null的时候,也就直接返回了false而不是抛出异常。
1.2 hashCode(Object o)
返回指定对象的哈希码值。如果参数为null,则返回0。而hashCode的计算则是使用了Objects的hash(Object… values)方法。它为所有指定的对象生成哈希码。它可以用于计算对象的哈希码,该哈希码基于多个实例字段。
我先上代码,运行指定对象不为空的情况
public class Test {
public static void main(String[] args) {
Person p1 = new Person("ddd",20);
System.out.println(Objects.hashCode(p1));
//generate自动生成的hashCode方法实际上就是Objects.hashCode方法
System.out.println(p1.hashCode());
}
static class Person{
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name) && Objects.equals(age, person.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
}
运行结果为
1.3 isNull(Object obj),nonNull(Object obj)
如果指定的对象为null,isNull()方法返回true。否则,它返回false。您还可以使用比较运算符== 来检查对象是否为null,例如,obj == null返回obj的true为null。
示例代码如下,Person的实体类和1.2中一样,就不再放代码了。
public static void main(String[] args) {
Person p1 = new Person("ddd",20);
Person p2 = null;
boolean result = Objects.isNull(p1);
System.out.println(result);
//p2为空
System.out.println(Objects.isNull(p2));
当p1不为空而p2为空时输出结果为
boolean nonNull(Object obj)
执行与isNull()方法相反的检查。
nonNull(Object obj)执行的就是与isNull()方法相反的检查,检查不为空。就不再多记录啦。
1.4 requireNonNull(T obj)
这个方法就是检查参数是否为null。如果参数为null,它会抛出一个NullPointerException异常。此方法就常用于设计验证方法和构造函数的参数。
代码:
public static void main(String[] args) {
Person p1 = new Person("ddd",20);
Person p2 = null;
System.out.println(Objects.requireNonNull(p1.getName()));
System.out.println(Objects.requireNonNull(p2.getName()));
}
}
运行结果
可以看出,当指定对象不为空时,能够正常输出,而当指定对象为空时,就是抛出空指针异常。如果不想看到异常的小伙伴,可以使用try-catch来观察运行效果
System.out.println(Objects.requireNonNull(p1.getName()));
//System.out.println(Objects.requireNonNull(p2.getName()));
try {
//p2为null
System.out.println(Objects.requireNonNull(p2.getName()));
}catch (NullPointerException e){
System.out.println(e.getMessage());
}
}
其实requireNonNull(T obj)的源码非常简单
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
就一个对传入的参数组一个简单的判断,为空就抛出异常不为空就返回传进来的参数,同时,使用的时泛型,所以调用起来比较灵活。
好了,对于Objects这个超类我就先记录这么多,有错误的话希望大家指出
2.java.lang.Math类
Math类的方法,参数的话不止我举例的一种,大家可以自己试试。所以,接下来方法举例的话我就随便选一种。然后一些三角函数的话就不多记录了,用的非常少的感觉,想用的话大家直接百度或者是看Api帮助文档就OK啦
2.1 abs(int a),返回绝对值的方法
public static void main(String[] args) {
//得到绝对值
System.out.println(Math.abs(-10));
System.out.println(Math.abs(10));
}
输出为
源码也比较简单就是进行(a < 0) ? -a : a;的判断
2.2 floor(double a),有一种向下取整的感觉
public static void main(String[] args) {
//Math.floor(double a)
System.out.println(Math.floor(11.8));
}
运行结果
2.3 max(double a, double b)与min(double a, double b),返回参数中的较大值与较小值
public static void main(String[] args) {
//输出较大值
System.out.println(Math.max(100,500));
//输出较小值
System.out.println(Math.min(1.5,-2.2));
}
输出结果
2.4 multiplyExact(int x, int y)返回参数的乘积,如果结果溢出 int则抛出异常。
public static void main(String[] args) {
System.out.println(Math.multiplyExact(100000,50));
}
输出为
源码
2.5 Math.random() 获得随机数返回带有正号的double值,大于或等于0.0且小于1.0
public static void main(String[] args) {
System.out.println(Math.random());
//指定范围随机数 0-10
System.out.println((int)(1+Math.random()*10));
}
输出为
2.6 pow(double a, double b)幂运算
public static void main(String[] args) {
System.out.println(Math.pow(2,3));
}
输出
2.7 round(double a) 四舍五入
public static void main(String[] args) {
System.out.println(Math.round(5.8));
System.out.println(Math.round(5.3))
}
输出
2.8 ceil(double a)向上取整
public static void main(String[] args) {
System.out.println(Math.ceil(5.8));
System.out.println(Math.ceil(5.3));
}
输出为
3.java.util.Arrays类
3.1 binarySearch方法
比较多,我就拿一种举例
public static void main(String[] args) {
int[] arr = {1,8,7,5,6,9,10,25};
//找到对应值在的下标
System.out.println(Arrays.binarySearch(arr,9));
//在指定范围中寻找对应值
System.out.println(Arrays.binarySearch(arr,2,6,9));
}
输出为
3.2 copyOf、copyOfRange数组复制
也比较多,所以只截取了一部分
实例代码
public static void main(String[] args) {
int[] arr = {1,8,7,5,6,9,10,25};
System.out.println("原数组长度为:"+arr.length);
System.out.println("原数组为:"+Arrays.toString(arr));
//使用零复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度
int[] arr2 = Arrays.copyOf(arr,15) ;
System.out.println("新数组长度为:"+arr2.length);
System.out.println("新数组为:"+Arrays.toString(arr2));
//复制arr 的下标为2到下标为5的数
int[] arr3 = Arrays.copyOfRange(arr,2,5);
System.out.println("新数组长度为:"+arr3.length);
System.out.println("新数组为:"+Arrays.toString(arr3));
}
输出为
3.3 sort、parallelSort的数组排序
示例代码
public static void main(String[] args) {
int[] arr = {1,8,7,5,6,9,10,25};
System.out.println("排序前:"+Arrays.toString(arr));
Arrays.sort(arr);
System.out.println("sout排序:"+Arrays.toString(arr));
Arrays.parallelSort(arr);
System.out.println("parallelSort排序:"+Arrays.toString(arr));
}
输出为
4. java.math.BigDecimal
通过在控制台运行0.1+0.2。 会发现float和double的运算误差。由于float类型和double类型在运算时可能会有误差,为了实现精确运算则需要借助java.math. BigDecimal 类加以描述
运算产生了误差,所以要精确的得到结果就需要需要通过BigDecimal创建对象,相要参与运算的小数的字符串形式作为参数。
示例代码:
public static void main(String[] args) {
//System.out.println(0.1+0.2);
BigDecimal b1 = new BigDecimal(0.1);
BigDecimal b2 = new BigDecimal(0.2);
//加法
BigDecimal b3 = b1.add(b2);
System.out.println(b3);
//减法
b3 = b1.subtract(b2);
System.out.println(b3);
//乘法
b3 = b1.multiply(b2);
System.out.println(b3);
//除法
b3 = b1.divide(b2);
System.out.println(b3);
}