JAVA中==和EQUALS的区别

1、 ==操作符
==操作符专门用来比较变量的值是否相同。

基本数据类型:比较的是他们的值是否相同。
引用数据类型:比较的是他们的内存地址是否同一地址。
引用类型对象变量其实是一个引用,它们的值是指向对象所在的内存地址,而不是对象本身。

2、 equals方法
equals方法常用来比较对象的内容是否相同。

Java当中所有的类都是继承于Object这个超类的,在Object类中定义的equals方法:

  1. public boolean equals(Object obj) {
  2. return (this == obj);
  3. }

未重写equals方法的类:Object中的equals方法实际使用的也是==操作符,比较的是他们的内存地址是否同一地址。
重写了equals方法的类:实现该类自己的equals方法比较逻辑(一般是比较对象的内容是否相同)。比如:
String:比较字符串内容,内容相同这相同;
Integer:比较对应的基本数据类型int的值是否相同(==操作符)。

点赞 ( 0 )

4 条评论

  1. https://free.whichav.win

    We are a gavgle of voluntteers andd opening a neww shheme in our community. Your web site provided us with helpful info tto work on. Youu haave perrformed aan imjpressive activity and our entire neighborhhood ill be thankful to you. xnxx

  2. love dolls usa

    Thanks for sharing

  3. love doll

    :mrgreen: :mrgreen: :mrgreen:

  4. Aldactazide from acne meds rx

    Aldactazide from acne meds rx

发表评论

人生在世,错别字在所难免,无需纠正。

插入图片
s
返回顶部