当前位置:   article > 正文

Equals 和 == 的区别_equals和==的区别

equals和==的区别

目录

一.区别

二.代码案例

三.String中Equals的源码分析


各位读者都知道Equals和== 都具有判断是否相等的作用,但这两个方法又有什么区别呢?具体区别如下

一.区别

1.最大的区别就是:==是一个比较运算符,而Equals是一种方法

2.先简单介绍一下==作用:==比较运算符既可以判断基本类型,又可判断引用类型

当判断基本类型时,判断的是值是否相等 eg.int i =10;double d = 10.0;则i == d 为true(这里涉及数据类型的自动转换,大家可以去看我写类型转换的文章)

当判断引用类型时,判断的是地址是否相等,即判断是否为同一个对像

这里我们在复习一下八种基本数据类型

整数:byte、short、int、long
浮点数:float、double
布尔类型 :boolean
字符类型:char(这里需要注意String不是基本类型)

这里我们具体介绍一下Equals方法:equals是Object类中的方法,只能判断应用类型,简单来说就是比较两个对象的地址是否相等,到这里感觉equals和==的方法类似,并且==的作用范围好像还比equals方法更大,但是在子类中往往重写该方法,用于判断内容是否相等,比如Integer,String

二.代码案例

下面举几个例子帮助大家理解(各位读者可以在自己的编译器上看看输出结果)

eg1.

  1. public class Equals01 {
  2. public static void main(String[] args) {
  3. A a = new A();
  4. A b = a;
  5. A c = b;
  6. System.out.println(a == c);
  7. B b2 = a;
  8. B b3 = new B();
  9. System.out.println(b2 == c);
  10. System.out.println(b2 == b3);
  11. Integer integer = new Integer(1);
  12. Integer integer1 = new Integer(1);
  13. System.out.println(integer.equals(integer1));
  14. Object o = new String();
  15. System.out.println(o instanceof String);
  16. String a1 = "asd";
  17. String a2 = "asd";
  18. System.out.println(a1.equals(a2));
  19. }
  20. }
  21. class B{ }
  22. class A extends B{ }

eg2: 判断两个 Person 对象的内容是否相等,如果两个 Person 对象的各个属性值都一样,则返回 true,反之 false。(重写equals方法

  1. public class Equalsexercis01 {
  2. public static void main(String[] args) {
  3. Person person = new Person("jack", 10, '男');
  4. Person person1 = new Person("jack", 10, '男');
  5. System.out.println(person.equals(person1));
  6. }
  7. }
  8. class Person{
  9. private String name;
  10. private int age;
  11. private char gender;
  12. //重写Object的Equals方法
  13. public boolean equals(Object obj){
  14. //判断如果比较的两个对象是同一个对象,则直接返回true
  15. if (this == obj){
  16. return true;
  17. }
  18. //类型判断
  19. if (obj instanceof Person){
  20. //类型转换
  21. //向下转型,因为需要得到obj的各个属性
  22. Person p = (Person)obj;
  23. return this.name.equals(p.name) && this.age == p.age && this.gender == this.gender;
  24. }
  25. return false;
  26. }
  27. public Person(String name, int age, char gender) {
  28. this.name = name;
  29. this.age = age;
  30. this.gender = gender;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setName(String name) {
  36. this.name = name;
  37. }
  38. public int getAge() {
  39. return age;
  40. }
  41. public void setAge(int age) {
  42. this.age = age;
  43. }
  44. public char getGender() {
  45. return gender;
  46. }
  47. public void setGender(char gender) {
  48. this.gender = gender;
  49. }
  50. }

三.String中Equals的源码分析

最后给大家分析一下String中Equals方法的源码

  1. public boolean equals(Object anObject) {//先判断传进来的对象是否相等,若相等直接输出true
  2. if (this == anObject) {
  3. return true;
  4. }
  5. if (anObject instanceof String) {
  6. //乍一看感觉这个if语句可能写错了,object不是顶级父类吗?那这个if判断不是永为false
  7. //其实这里的instanceof是判断的运行类型
  8. //不理解的读者可以看一下我写的多态的那篇文章,希望对你有所帮助
  9. String anotherString = (String)anObject;
  10. int n = value.length;
  11. if (n == anotherString.value.length) {
  12. char v1[] = value;
  13. char v2[] = anotherString.value;
  14. int i = 0;
  15. while (n-- != 0) {
  16. if (v1[i] != v2[i])//判断字符串是否相等
  17. return false;
  18. i++;
  19. }
  20. return true;
  21. }
  22. }
  23. return false;
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/876650
推荐阅读
相关标签
  

闽ICP备14008679号