Object
public static void main(String[] args) throws IOException {
student s1 = new student();
student s2 = new student();
boolean result1 = s1.equals(s2);
System.out.println(result1);
}
package System;
import java.util.Objects;
public class student {
public String name;
public int age;
public student() {
}
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
student student = (student) o;
return age == student.age && Objects.equals(name, student.name);
}
}
方法名 | 说明 |
---|
public Object() | 空参构造 |
public String toString() | 返回对象的字符串表示形式 |
public boolean equals(Object obj) | 比较两个对象是否相等 |
protected Object clone(int a) | 对象克隆 |
克隆
package oneday;
import java.util.StringJoiner;
public class ObjectDemo1 implements Cloneable{
private int id;
private String username;
private String password;
private String path;
private int[] data;
public ObjectDemo1() {
}
public int getId() {
return id;
}
public ObjectDemo1(int id, String username, String password, String path, int[] data) {
this.id = id;
this.username = username;
this.password = password;
this.path = path;
this.data = data;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
public String toString(){
return "角色编号为:" + id + ",用户名:" + username + ",密码:" + password + ",游戏图片:" + path + ",进度:" + arrToString();
}
public String arrToString(){
StringJoiner sj = new StringJoiner(",","[","]");
for (int i = 0; i < data.length; i++) {
sj.add(data[i] + "");
}
return sj.toString();
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package oneday;
public class ObjectDemo2 {
public static void main(String[] args) throws CloneNotSupportedException {
int[] data = {1,2,3,4,5,6,7,8,9};
ObjectDemo1 u1 = new ObjectDemo1(1,"zhangsan","1234qwer","girl11",data);
ObjectDemo1 u2 = (ObjectDemo1) u1.clone();
int[] arr = u1.getData();
arr[0] = 100;
System.out.println(u1);
System.out.println(u2);
}
}
浅克隆
package oneday;
import java.util.StringJoiner;
public class ObjectDemo1 implements Cloneable{
private int id;
private String username;
private String password;
private String path;
private int[] data;
public ObjectDemo1() {
}
public int getId() {
return id;
}
public ObjectDemo1(int id, String username, String password, String path, int[] data) {
this.id = id;
this.username = username;
this.password = password;
this.path = path;
this.data = data;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
public String toString(){
return "角色编号为:" + id + ",用户名:" + username + ",密码:" + password + ",游戏图片:" + path + ",进度:" + arrToString();
}
public String arrToString(){
StringJoiner sj = new StringJoiner(",","[","]");
for (int i = 0; i < data.length; i++) {
sj.add(data[i] + "");
}
return sj.toString();
}
@Override
protected Object clone() throws CloneNotSupportedException {
int[] data = this.data;
int[] newData = new int[data.length];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
ObjectDemo1 u = (ObjectDemo1) super.clone();
u.data = newData;
return u;
}
}
package oneday;
public class ObjectDemo2 {
public static void main(String[] args) throws CloneNotSupportedException {
int[] data = {1,2,3,4,5,6,7,8,9};
ObjectDemo1 u1 = new ObjectDemo1(1,"zhangsan","1234qwer","girl11",data);
ObjectDemo1 u2 = (ObjectDemo1) u1.clone();
int[] arr = u1.getData();
arr[0] = 100;
System.out.println(u1);
System.out.println(u2);
}
}
深克隆
Objects
方法名 | 说明 |
---|
public static boolean equals(Objec a,Object b) | 先做非空判断,比较两个对象 |
public static boolean isNull(Object obj) | 判断对象是否为null,为null返回true,反之 |
public static boolean nonNull(Object obj) | 判断对象是否为null,跟isnull结果相反 |
package test8;
import java.util.Objects;
public class Test {
public static void main(String[] args) {
Student s1 = null;
Student s2 = new Student(1,"zhangsan",23);
Student s3 = new Student();
Student s4 = null;
boolean result = Objects.equals(s1,s2);
System.out.println(result);
System.out.println(Objects.isNull(s3));
System.out.println(Objects.isNull(s4));
System.out.println(Objects.nonNull(s4));
}
}
BigInteger
public static void main(String[] args) {
Random r = new Random();
BigInteger bd1 = new BigInteger(4,r);
System.out.println(bd1);
BigInteger bd2 = new BigInteger("100");
System.out.println(bd2);
BigInteger bd3 = new BigInteger("100",2);
System.out.println(bd3);
}
//public BigInteger(int num, Random rnd) 获取随机大整数,范围:[0 ~ 2的num次方 -1]
//public BigInteger(String val) 获取指定的大整数
//public BigInteger(String val,int radix) 获取指定进制的大整数
public static void main(String[] args) {
BigInteger bd1 = BigInteger.valueOf(10);
BigInteger bd2 = BigInteger.valueOf(5);
BigInteger bd3 = bd1.add(bd2);
System.out.println(bd3);
BigInteger[] arr = bd1.divideAndRemainder(bd2);
System.out.println(arr[0]);
System.out.println(arr[1]);
boolean result = bd1.equals(bd2);
System.out.println(result);
BigInteger bd4 = bd1.pow(2);
System.out.println(bd4);
BigInteger bd5 = bd1.max(bd2);
System.out.println(bd5);
System.out.println(bd5 == bd1);
System.out.println(bd5 == bd2);
BigInteger bd6 = BigInteger.valueOf(1000);
int i = bd6.intValue();
double j = bd6.doubleValue();
System.out.println(i);
System.out.println(j);
}
方法名 | 说明 |
---|
public BigInteger add(BigInteger val) | 加法 |
public BigInteger subtract(BigInteger val) | 减法 |
public BigInteger multiply(BigInteger val) | 乘法 |
public BigInteger divide(BigInteger val) | 除法,获取商 |
public BigInteger[] divideAndRemainder(BigInteger val) | 除法,获取商和余数 |
public boolean equals(Object x) | 比较是否相同 |
public BigInteger pow(int exponent) | 次幂 |
public BigInteger max/min(BigInteger val) | 返回较大值/较小值 |
public int intValue(BigInteger val) | 转为int类型整数,超出范围数据错误 |
BigDecimal
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal(0.01);
BigDecimal bd2 = new BigDecimal(0.09);
System.out.println(bd1);
System.out.println(bd2);
BigDecimal bd3 = new BigDecimal("0.01");
BigDecimal bd4 = new BigDecimal("0.09");
BigDecimal bd5 = bd3.add(bd4);
System.out.println(bd3);
System.out.println(bd4);
System.out.println(bd5);
BigDecimal bd6 = BigDecimal.valueOf(10);
System.out.println(bd6);
}
方法名 | 说明 |
---|
public static BigDecimal valueOf(double val) | 获取对象 |
public BigDecimal add(BigDecimal val) | 加法 |
public BigDecimal subtract(BigDecimal val) | 减法 |
public BigDecimal multiply(BigDecimal val) | 乘法 |
public BigDecimal divide(BigDecimal val) | 除法 |
public BigDecimal divide(BigDecimal val,精确几位,舍入模式) | 除法 |
public static void main(String[] args) {
BigDecimal bd1 = BigDecimal.valueOf(10.0);
BigDecimal bd2 = BigDecimal.valueOf(2.0);
BigDecimal bb1 = BigDecimal.valueOf(3.0);
BigDecimal bd3 = bd1.add(bd2);
System.out.println(bd3);
BigDecimal bd4 = bd1.subtract(bd2);
System.out.println(bd4);
BigDecimal bd5 = bd1.multiply(bd2);
System.out.println(bd5);
BigDecimal bd6 = bd1.divide(bd2);
System.out.println(bd6);
BigDecimal bd7 = bd1.divide(bb1, 2, RoundingMode.HALF_UP);
System.out.println(bd7);
}
Tips
以上学习内容均来自于B站黑马程序员