Java_SE之Object类详解

Terwer Java SE评论233字数 2399阅读7分59秒阅读模式

相等性的比较(==)

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

java.lang.Object 类

java.lang 包在使用时无需显式导入,编译时由编译器帮助我们导入。文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

API(Application Programming Interface)

应用编程接口。文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

toString

当打印引用时,实际上会打印引入所指对象的 toString() 方法的返回值。因为每个类都直接或者间接的继承自 Object ,而 Object 类中定义了 toString() 方法,因此,每个类都有 toString() 这个方法。文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

例子:文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

public class ObjectTest {
    public static void main(String[] args) {
        Object object = new Object();

        System.out.println(object);
        System.out.println(object.toString());

        String str = "aaa";
        System.out.println(str);
        System.out.println(str.toString());

        Student student = new Student();
        System.out.println(student);
        System.out.println(student.toString());
    }
}

class Student {
    public String toString() {
        return "Hello World";
    }
}

结果:文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

java.lang.Object@2c8d66b2
java.lang.Object@2c8d66b2
aaa
aaa
Hello World
Hello World

进制的表示

关于进制的表示:文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

16进制,逢16进一,16进制的数字包括:0~9,A,B,C,D,E,F文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

例子:文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

int v16 = 0xAB2F;
System.out.println(v16);

int result = 10 * (int) Math.pow(16, 3) + 11 * (int) Math.pow(16, 2) + 2 * 16 + 15;
System.out.println(result);

// 43823
// 43823

equals

看看官方文档文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

Indicates whether some other object is "equal to" this one.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

The equals method implements an equivalence relation on non-null​ object references:文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

  • For any non-null reference value x, x.equals(null) should return false.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

Params:
obj – the reference object with which to compare.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

Returns:
true if this object is the same as the obj argument; false otherwise.文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

equals方法,定义在 Object 类中,因此 Java 中的每个类都具有该方法,对于 Object 类的 equals() 方法来说,它是判断调用 equals 方法的引用与传进来的引用是否一致,即这两个引用是否指向的是同一个对象。文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

对于 Object 类的 equals 方法来说,它等价于 ==文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

对于 String 类的 equals 方法来说,它是判断当前字符串与传进来的字符串内容是否一致。文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

对于 String​ 对象的相等性来说,请使用 equals 方法,而不要使用 ==文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

文章源自浅海拾贝-https://blog.terwergreen.com/java_se-object-class-detailed-explanation-1feegn.html

相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: