Javase第十二讲:Object类

1、相等性的比较(==)

1) 对于原生数据类型来说,比较的是左右两边的值是否相等。
2)对于引用类型来说,比较左右两边的引用是否指向同一个对象,或者说左右两边的引用地址是否相同。

2、java.lang.Object类

1):java.lang.Object类,java.lang包在使用时无需显示导入【java中这个包不需要显示导入,因为这个包很常用,Sun公司自动导入了】,编译时又编译器自动导入。

2): API(Application Programming Interface),应用编程接口。

public class HelloWorld {
	public static void main(String args[]) {
			Object object=new Object();
			System.out.println(object);
			System.out.println(object.toString());
	}
}

在这里插入图片描述

3、当打印引用时,实际上会打印出引用所指对象的toString()方法的返回值,因为每个类都直接或间接地继承自Object,而Object类中定义了toString(),因此每个类都有toString()的这个方法。

public class HelloWorld {
	public static void main(String args[]) {
			Object object=new Object();
			System.out.println(object);
			System.out.println(object.toString());
			
			String string="hello world";
			System.out.println(string);
			System.out.println(string.toString());
	}
}

在这里插入图片描述

注意:String string=“hello world”;注意字符串String是一个特殊的类,在java中只有字符串可以这样直接的赋值,其它对象都是通过new的方式出来的,哪怕是单例也是new出来的。也可以这样写String string2=new String(“hello world”);

4、==的运用

public class HelloWorld {
	public static void main(String args[]) {
			Object object=new Object();
			Object object2=new Object();
			System.out.println(object==object2);
			System.out.println("----------------------------");
			
			String string=new String("hello world");
			String string2=new String("hello world");
			System.out.println(string==string2);
            System.out.println("-----------------------------");
           /*
           *String Pool字符串池,string在String Pool创建了“hello world”的对象,string2就String Pool中不再创建“hello world”对象,但是会在堆中创建对象。即他们最终都会在堆(heap)中创建对象,然后把各自地址赋给string和string2。所以他们输出为false。
           */
           
           String string3="hello world";
           String string4="hello world";
           System.out.println(string3==string4);
           System.out.println("------------------------------");
           /*
           *String Pool字符串池,由于string3已经在String Pool中创建一个“hello world”的对象了,string4由于检查String Pool中已经有string3生成的那个“hello  world”对象了,所以string4不再生成对象,直接把地址返回赋给string4,此时他们指向同一个对象,所以输出true。

           */
           String string5="hello world";
           String string6=new String("hello world");
           System.out.println(string5==string6);
           System.out.println("------------------------------");
           
           String string7="hello";
           String string8="he";
           String string9="llo";
           System.out.println(string7==string8+string9);
           System.out.println("------------------------------");
           
           System.out.println(string7=="he"+"llo");
	}
}

在这里插入图片描述
【说明】:==用来比较两个对象的地址是否相等,:String Pool是在内存栈中,如果是new出一个实例的话是在内存的堆(heap)中生成一个对象。这两个在内存中的位置是不一样的。
String Pool(字符串池),Java中需要维护字符串池的是因为在实际开发中经常会遇到String,所以不用每次都在heap中去生成一个对象,而且这个对象用完一般都是丢掉的,所以需要维护字符串池,而且一般字符串写法用这种形式:String str = “hello world”。

     /*
     *虽然string和string2都是hello world,但是它们都new 了一个对象,两个对象的地址是不一样的,所以返回false
     */
 	String string=new String("hello world");
			String string2=new String("hello world");
			System.out.println(string==string2);
            System.out.println("-----------------------------");
           

在这里插入图片描述

5、equals()

1)equals() 是定义在Object类中的
2)equals()的返回类型是boolean型的
判断当前这个对象跟其传进来的对象是否相等。

String string=new String("hello world");
			String string2=new String("hello world");
			System.out.println(string.equals(string2));

输出结果:true

在这里插入图片描述
3):equals()方法,该方法定义在Object类当中,因此Java中的每个类都具有该方法,对于Object类的equals()方法来说,它是判断调用equals()方法的引用与传进来的引用是否一致,即这两个引用是否指向的是同一个对象。对于Object类的equals()方法来说,它等价于==。

4):对于Object类的equals()方法来说是判断两个引用是否一致,是否指向同一个对象,而对于其他继承了Object类的其他类,如果它重写了Object类的equals()方法,它才是判断其内容是否一致,如果没有重写的话则和Object类的equals()方法判断方式一样。

public class HelloWorld {
	public static void main(String args[]) {
			Student student=new Student(18);
		Student student2=new Student(18);
		System.out.println(student.equals(student2));
	}
}
class Student{
	int age;
	public Student(int age){
		this.age=age;
	}
}

输出结果:false


public class HelloWorld {
	public static void main(String args[]) {
			Student student=new Student(18);
		Student student2=new Student(18);
		System.out.println(student.equals(student2));
	}
}

class Student{
	Integer age;
	public Student(Integer age){
		this.age=age;
	}
	public boolean equals(Object object){
		if(this==object){
			return true;
		}
		if (object instanceof Student) {//instanceof用来测试一个对象是否为一个类的实例
			Student  student4=(Student)object;
			if (student4.age.equals(this.age)) {
				return true;
			}
		}
		return false;	
	}
}

输出结果:true

6、String

String是常量,其对象一旦创建完毕就无法改变。当使用+拼接字符串时,会生成新的String对象,而不是向原有的String对象追加内容。

发布了45 篇原创文章 · 获赞 38 · 访问量 2143

猜你喜欢

转载自blog.csdn.net/qq_44830627/article/details/105113677