📚1. ==
==
是一个比较远算符
用法:
1.==
:既可以判断基本类型,又可以判断引用类型
2.==
:如果判断基本类型,判断的是值是否相等。
int num1 = 10;
double num2 = 10.0;
//基本数据类型,判断值是否相等
System.out.println(num1 == num2);//true
3.==
:如果判断引用类型,判断的是地址是否相等,即判定是不是同一个对象。
public class Equals01 {
public static void main(String[] args) {
A a = new A();
A b = a;
A c = b;
System.out.println(a == c);//true
System.out.println(b == c);//true
B bObj = a;
//注意结果
System.out.println(bObj == c);//true
//依旧是同一个对象,编译类型是B,但运行类型依旧是A,指向的是同一个对象。
}
}
class B {}
class A extends B {}
📚2. equals 方法
2.1 equals
:是Object类中的方法,只能判断引用类型
🍀拓展:
equals方法源码怎么查看?
"hello".equals("abc");
ldea如何查看Jdk 源码:
1.一般来说IDEA配置好JDK以后,jdk的源码也就自动配置好了
2.如果没有的话点击菜单File –> Project Structure –> SDKs –> Sourcepath然后点击右侧绿色的加号。
3.在需要查看某个方法源码时,将光标放在该方法,输入ctrl+ b即可或者在方法上点击右键->go to -> Declaration or …
接下来我们看看它的源码。
equals源码:
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
2.2:默认判断的是地址是否相等,子类中往往重写该方法,用于判断内容是否相等。比如lnteger
,String
【看看String
和 Integer
的equals
源代码】
Object 类的 equals方法 :
//Object 的 equals 方法默认就是比较对象地址是否相同
//也就是判断两个对象是不是同一个对象.
public boolean equals(Object obj) {
return (this == obj);
}
Integer 的 equals 方法:
//从源码可以看到 Integer 也重写了 Object 的 equals 方法, //变成了判断两个值是否相同
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
例如:
Integer integer1 = new Integer(1000);
Integer integer2 = new Integer(1000);
System.out.println(integer1 == integer2);//false
System.out.println(integer1.equals(integer2));//true
String str1 = new String("Barid");
String str2 = new String("Barid");
System.out.println(str1 == str2);//false
System.out.println(str1.equals(str2));//true
🍀equals方法练习:
练习一
分析下面代码,写出自己认为的输出值:
public class EqualsExercise {
public static void main(String[] args) {
Person_ p1 = new Person_();
p1.name = "hspedu";
Person_ p2 = new Person_();
p2.name = "hspedu";
System.out.println(p1==p2);
System.out.println(p1.name .equals( p2.name));
System.out.println(p1.equals(p2));
String s1 = new String("asdf");
String s2 = new String("asdf");
System.out.println(s1.equals(s2));
System.out.println(s1==s2);
}
}
class Person_ {//类
public String name;
}
答案:
public class EqualsExercise{
public static void main(String[] args) {
Person_ p1 = new Person_();
p1.name = "hspedu";
Person_ p2 = new Person_();
p2.name = "hspedu";
//1.判断引用类型,及判断两个对象(地址)是否相等。
System.out.println(p1==p2); //False
//2.判断内容
System.out.println(p1.name .equals( p2.name));//T
//3,p1是person类的,是调用Object的equals,判断该两个对象是否相等
System.out.println(p1.equals(p2));//False
String s1 = new String("asdf");
String s2 = new String("asdf");
//判断该equals是否重写,若重写则比较内容(值)
System.out.println(s1.equals(s2));//T
System.out.println(s1==s2); //F
}
}
class Person_ {//类
public String name;
}
练习二
public class EqualsExercise03 {
public static void main(String[] args) {
int it = 65;
float fl = 65.0f;
System.out.println("65 和 65.0f 是否相等?" + (it == fl));//T
char ch1 = 'A'; char ch2 = 12;
System.out.println("65 和'A'是否相等?" + (it == ch1));//T
System.out.println("12 和 ch2 是否相等?" + (12 == ch2));//T
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1 和 str2 是否相等?"+ (str1 == str2)); //F
System.out.println("str1 是否 equals str2?"+(str1.equals(str2)));//T
//System.out.println("hello" == new java.sql.Date()); //编译错误
}
}
📚3. 重写 equals 方法
视频分析:·1韩顺平Java_equals课堂练习1
看下面代码:
public class EqualsExercise {
public static void main(String[] args) {
Person person1 = new Person("jack", 10, '男');
Person person2 = new Person("jack", 10, '男');
System.out.println(person1.equals(person2));//true
}
}
class Person { //extends Object
private String name;
private int age;
private char gender;
//重写Object的equals方法
public boolean equals(Object obj) {
//判断如果比较的两个对象是同一个对象,则返回true
if (this == obj) {//this表示当前的对象,是指调用this的对象
return true;
}
//类型的判断
if (obj instanceof Person) {//是Person,我们才比较
//将进行 向下转型,因为我需要得到obj的各个属性
Person p=(Person) obj;
return this.name.equals(p.name)&&this.age== p.age&&this.gender==p.gender;//比较内容
}
return false;
}
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
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;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
📚4. hashCode 方法
- 提高具有哈希结构的容器的效率。
- 两个引用,如果指向的是同一个对象,则哈希值肯定是一样的。
- 两个引用,如果指向的是不同对象,则哈希值是不一样的。
- 哈希值主要根据地址号来的,不能完全将哈希值等价于地址。
🍀案例演示:
public class HashCode_ {
public static void main(String[] args) {
AA aa = new AA();
AA aa2 = new AA();
AA aa3 = aa;
System.out.println("aa.hashCode()=" + aa.hashCode());
System.out.println("aa2.hashCode()=" + aa2.hashCode());
System.out.println("aa3.hashCode()=" + aa3.hashCode());
}
}
class AA {
}
🍀运行结果:
aa.hashCode()=460141958
aa2.hashCode()=1163157884
aa3.hashCode()=460141958
📚5. toString 方法
默认返回:全类名+@+哈希值的十六进制,子类往往重写 toString 方法,用于返回对象的属性信息。
🍀重写 toString 方法:
重写 toString 方法打印对象或拼接对象时,都会自动调用该对象的 toString 形式.
当直接输出一个对象时,toString 方法会被默认的调用, 比如 System.out.println(monster); 就会默认调用monster.toString()
public class ToString_ {
public static void main(String[] args) {
/*
Object 的 toString() 源码
(1)getClass().getName() 类的全类名(包名+类名 )
(2)Integer.toHexString(hashCode()) 将对象的 hashCode 值转成 16 进制字符串
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
*/
Monster monster = new Monster("小妖怪", "巡山的", 1000);
System.out.println(monster.toString() + " hashcode=" + monster.hashCode());
System.out.println("==当直接输出一个对象时,toString 方法会被默认的调用==");
System.out.println(monster); //等价 monster.toString()
}
}
class Monster {
private String name;
private String job;
private double sal;
public Monster(String name, String job, double sal) {
this.name = name;
this.job = job;
this.sal = sal;
}
//重写 toString 方法, 输出对象的属性
//使用快捷键即可 alt+insert -> toString
@Override
public String toString() { //重写后,一般是把对象的属性值输出,当然程序员也可以自己定制
return "Monster{" +
"name='" + name + '\'' +
", job='" + job + '\'' +
", sal=" + sal +
'}';
}
运行结果:
Monster{name='小妖怪', job='巡山的', sal=1000.0} hashcode=460141958
==当直接输出一个对象时,toString 方法会被默认的调用==
Monster{name='小妖怪', job='巡山的', sal=1000.0}
📚6. finalize 方法
- 当对象被回收时,系统自动调用该对象的 finalize 方法。子类可以重写该方法,做一些释放资源的操作
- 什么时候被回收:当某个对象没有任何引用时,则 jvm 就认为这个对象是一个垃圾对象,就会使用垃圾回收机制来销毁该对象,在销毁该对象前,会先调用 finalize 方法。
- 垃圾回收机制的调用,是由系统来决定(即有自己的 GC 算法), 也可以通过 System.gc() 主动触发垃圾回收机制,测试:Car [name]
提示: 我们在实际开发中,几乎不会运用 finalize , 所以更多就是为了应付面试。想要了解更多请看视频:韩顺平Java_finalize
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/199747.html