Object类:
public String toString();
Returns a string representation of the object.
基本作用:返回对象的字符串表示形式
存在的意义:让子类重写,以便返回子类对象的内容
public class student {
private String name;
private 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//重写toString方法
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
基本作用:判断两个对象是否相等(比较的是对象的地址是否相同)
存在的意义:让子类重写,以便于比较对象的内容是否相同
public class test {
public static void main(String[] args) {
student s1=new student("hhh",18);
System.out.println(s1);
student s2=new student("hhh",18);
System.out.println(s1.equals(s2));//false
}
}
我们可以重写equals方法:
/*@Override
public boolean equals(Object obj)
{
student s=(student)obj;
if(this.name.equals(s.name)&&this.age==s.age)
{
return true;
}
else return false;
}*/
s1==this s2==o
@Override
public boolean equals(Object o) {
//判断两个对象地址是否一样,一样直接返回true
if (this == o) return true;
//判断o是null直接返回false或者比较者和被比较者类型不一样,直接返回false
if (o == null || getClass() != o.getClass()) return false;
student student = (student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
这样就会返回true
protected Object clone();
Creates and returns a copy of this object.
当对象调用这个方法时,这个方法会复制一个一模一样的新对象返回(对象地址也一样)
直接在main函数调用clone会报错,因为clone是protected修饰,要在其他包下的Object的子类才能使用,但是main函数是程序的入口,不是子类。
所以要在student类下重写clone方法,student是object的子类,所以可以调用。
然后student类和test类又在同一个包下,所以main函数下就可以访问clone函数了。
public class test {
public static void main(String[] args) {
student s1=new student("hhh",18);
System.out.println(s1);
student s2=new student("hhh",18);
System.out.println(s1.equals(s2));
student s3=(student) s1.clone();//error
}
}
student类重写clone
//Cloneable是一个标记接口
//标志你重写了clone函数,不然JVM虚拟机不知道,会报错
public class student implements Cloneable{
private String name;
private 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
protected Object clone() throws CloneNotSupportedException {
return super.clone();//重新调用父类Object的clone函数
}
}
成功:
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
student s1=new student("hhh",18);
System.out.println(s1);
student s2=new student("hhh",18);
System.out.println(s1.equals(s2));//false
student s3=(student) s1.clone();
System.out.println(s3.getName());//hhh
}
}
Objects类:
public static boolean equals(Object a, Object b)
public static boolean equals(Object a, Object b) { return (a == b)//先判断地址是否一样 || (a != null && a.equals(b)); }
Returns
true
if the arguments are equal to each other andfalse
otherwise. Consequently, if both arguments arenull
,true
is returned. Otherwise, if the first argument is notnull
, equality is determined by calling theequals
method of the first argument with the second argument of this method. Otherwise,false
is returned.先做非空判断,再比较两个对象是否相同
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
student s1=new student("hhh",18);
System.out.println(s1);
student s2=new student("hhh",18);
System.out.println(s1.equals(s2));//false
student s3=(student) s1.clone();
System.out.println(s3.getName());//hhh
System.out.println(s3);
System.out.println(s1);
System.out.println(Objects.equals(s1, s2));//true
}
}
public static boolean isNull(Object obj)
public static boolean isNull(Object obj) { return obj == null; }
Returns
true
if the provided reference isnull
otherwise returnsfalse
.判断对象是否为空,空返回true
s3=null;
System.out.println(Objects.equals(s1,s3));
System.out.println(Objects.isNull(s3));//true
public static boolean nonNull(Object obj)
Returns
true
if the provided reference is non-null
otherwise returnsfalse
.
包装类:
把基本的数据类型的数据包装成对象
Integer类:
public String toString()
Returns a
String
object representing thisInteger
's value.把int类型的数据变成字符串
public static int parseInt(String s) throws NumberFormatException
Parses the string argument as a signed decimal integer
把字符串类型变成int类型
public class test {
public static void main(String[] args) {
Integer t1=Integer.valueOf(12);//把12包装成类
System.out.println(t1);//12
//自动装箱:自动把基本类型的数据转换成对象
Integer a=3;
//自动拆箱:可以自动把包装类型的对象转换成对应的基本数据类型
int a1=a;
//泛型和集合不支持基本数据类型,只能支持引用数据类型
ArrayList<Integer>list =new ArrayList<>();
list.add(12);
list.add(24);
System.out.println(list);
//1:把基本数据类型转成字符串
String s=Integer.toString(3);//把3变成字符串
System.out.println(s+1);//31
String s2=a.toString();
System.out.println(s2+33);//333
String s3=a+"";
//2:把字符串类型变成int类型
String ss="333";
//int b=Integer.parseInt(ss);
int b=Integer.valueOf(ss);
System.out.println(b+1);//334
String ss2="33.5";
// double d=Double.parseDouble(ss2);
double d=Double.valueOf(ss2);
System.out.println(d+1);//34.5
}
}
StringBuilder类:
构造函数:
public StringBuilder append(任意类型)
添加数据并返回StringBuilder对象本身
public class test {
public static void main(String[] args) {
StringBuilder s=new StringBuilder("hhh");
//1:拼接内容
s.append(12);
s.append("java");
s.append(true);
//支持链式编程
s.append(666).append("sss");
System.out.println(s);
}
}
public StringBuilder reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
将对象的内容反转
public class test {
public static void main(String[] args) {
StringBuilder s=new StringBuilder("hhh");
//1:拼接内容
s.append(12);
s.append("java");
s.append(true);
//支持链式编程
s.append(666).append("sss");
System.out.println(s);//hhh12javatrue666sss
s.reverse();
System.out.println(s);//sss666eurtavaj21hhh
}
}
public int length()
Returns the length (character count).
返回字符串长度
//返回字符串长度
System.out.println(s.length());//19
public String toString()
Returns a string representing the data in this sequence. A new
String
object is allocated and initialized to contain the character sequence currently represented by this object. ThisString
is then returned. Subsequent changes to this sequence do not affect the contents of theString
.把StringBuilder对象转换成String类型
String s2=s.toString();
System.out.println(s2);//sss666eurtavaj21hhh
应用:
把整形数组变成[]的String类型
public class StringBuilder2 {
public static void main(String[] args) {
String s =getArrData(new int[]{11,22,33});
System.out.println(s);
}
public static String getArrData(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
sb.append(arr[i]).append("]");
} else {
sb.append(arr[i]).append(",");
}
}
String s = sb.toString();
return s;
}
}
结果:[11,22,33]
为什么操作字符串建议使用StringBuilder而不用String
对于字符串的相关操作,如频繁的拼接,修改等,建议使用StringBuilder,效率更高
StringBuffer的用法和StringBuilder是一模一样的
但是StringBuffer是线程安全的,StringBuffer是线程不安全的
StringJoiner类 :
好处:不仅能提高字符串的操作效率,并且有些场景使用它操作字符串,代码会更加简洁
Constructor
Description
StringJoiner(CharSequence delimiter)
Constructs a
StringJoiner
with no characters in it, with noprefix
orsuffix
, and a copy of the supplieddelimiter
.//创建一个StringJoiner对象,指定拼接时的间隔符号
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
Constructs a
StringJoiner
with no characters in it using copies of the suppliedprefix
,delimiter
andsuffix
.//创建一个StringJoiner对象,指定拼接时的间隔符号,开始符号,结束符号
public StringJoiner add(CharSequence newElement)
Adds a copy of the given
CharSequence
value as the next element of theStringJoiner
value. IfnewElement
isnull
, then"null"
is added.添加数据,并返回对象本身
public class StringBuilder3 {
public static void main(String[] args) {
String s =getArrData(new int[]{11,22,33});
System.out.println(s);//[11,22,33]
}
public static String getArrData(int[] arr) {
StringJoiner sj=new StringJoiner(",","[","]");
for (int i = 0; i < arr.length; i++) {
sj.add(arr[i]+"");//由于sj只能操作字符串,所以arr[i]+""把int类型变成字符串类型
}
String s = sj.toString();
return s;
}
}