首页 > 编程语言 >Java_SE之Object类详解

Java_SE之Object类详解

时间:2022-10-17 10:58:07浏览次数:42  
标签:Java Object equals System println true SE out

相等性的比较(==)

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

java.lang.Object 类

java.lang 包在使用时无需显式导入,编译时由编译器帮助我们导入。

API(Application Programming Interface)

应用编程接口。

toString

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

例子:

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";
    }
}

结果:

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

进制的表示

关于进制的表示:

16进制,逢16进一,16进制的数字包括:0~9,A,B,C,D,E,F

例子:

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

看看官方文档

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null​ object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.

  • 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.

  • 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.

  • 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.

  • For any non-null reference value x, x.equals(null) should return false.

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).

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.

Params:
obj – the reference object with which to compare.

Returns:
true if this object is the same as the obj argument; false otherwise.

equals方法,定义在 Object 类中,因此 Java 中的每个类都具有该方法,对于 Object 类的 equals() 方法来说,它是判断调用 equals 方法的引用与传进来的引用是否一致,即这两个引用是否指向的是同一个对象。

对于 Object 类的 equals 方法来说,它等价于 ==

对于 String 类的 equals 方法来说,它是判断当前字符串与传进来的字符串内容是否一致。

对于 String​ 对象的相等性来说,请使用 equals 方法,而不要使用 ==

标签:Java,Object,equals,System,println,true,SE,out
From: https://www.cnblogs.com/tangyouwei/p/java_se-object-class-detailed-explanation-1feegn.html

相关文章

  • 局部敏感哈希(Locality Sensitive Hashing)和MinHash介绍与实例
    在实际应用中,我们所面对的数据是海量的,并且有着很高的维度。在对数据的各种操作中,查询操作是最常见的一种,这里的查询是指输入一个数据,查找与其相似的数据,那么怎样快速地......
  • Java_SE_第八讲:理解面向对象程序设计
    break语句:经常用在循环语句中,用于跳出整个循环,执行循环后面的代码。continue语句:经常用在循环语句中,用于跳出当前的这个循环(或者是跳出本次循环),开始下一次循环的执......
  • java 自动补齐数值至指定位数
    如流水号需要在数字前自动补齐0,至指定位数。如果已经达到了指定位数,不会补齐,返回原数值。自定义/***自动补齐位数*@paramlength补齐后的位数*@paramnum待......
  • Java_SE_第九讲:面向对象之封装
    面向对象程序设计的三大基本特征:继承(Inheritence​)、封装(Encapsulation​)、多态(Polymorphism​)封装:类包含了数据与方法,将数据与方法放在一个类中就构成了封装。如何......
  • Java_SE之Java_SE平台与JDK
    Java平台JavaSE:JavaPlatform,StandardEditionJavaSE是一个计算平台,用于为桌面和服务器环境开发和部署可移植代码。JavaSE以前称为Java2Platform,StandardEd......
  • Sproxy进入后无Service Interfaces栏位
    货铺QQ群号:834508274进群统一修改群名片,例如BJ_ABAP_森林木。群内禁止发广告及其他一切无关链接,小程序等,进群看公告,谢谢配合 不修改昵称会被不定期踢除,谢谢配合新系统做接......
  • 【Azure 媒体服务】记使用 Media Servic
    问题描述在使用AzureMediaService的官网示例(media-services-v3-java-->AudioAnalytics-->AudioAnalyzer )代码的过程中,根据配置添加了EventHub和StorageAccount,使......
  • MyEclipse技术全面解析——Spring IDE,改变应用程序研发
    通过MyEclipse SpringIDE,您可以获得从内置库到bean开发工具的Spring框架开发所需的所有东西,甚至还可以获得用于启动Spring项目的Springscaffolding。但Spring只是其中的......
  • SE73 上传字体
    有时候smartforms打印需要第三方字体SE73上传字体:(自行准备ttf格式的字体文件)之后在style创建的时候就可以选此字体了其他信息可以参考note:1605966-UploadTrueTypef......
  • HashSet存储重复元素流程图和LinkedHashSet集合
    代码:publicstaticvoidmain(String[]args){HashSet<String>set=newHashSet<>();Strings1=newString("abc");Strings2=newString("abc");se......