远方的灯塔 - 专注于服务端技术分享 远方的灯塔 - 专注于服务端技术分享
首页
  • Java SE
  • Struts2
  • Hibernate
  • MyBatis
  • JAX-WS
  • 并发
  • 分布式
  • Git
  • 文章分类
  • 文章标签
  • 文章归档
  • 《C程序设计语言》
心情随笔
友情链接
给我留言 (opens new window)
关于我
GitHub (opens new window)

Terwer Green

一个后端老菜鸟
首页
  • Java SE
  • Struts2
  • Hibernate
  • MyBatis
  • JAX-WS
  • 并发
  • 分布式
  • Git
  • 文章分类
  • 文章标签
  • 文章归档
  • 《C程序设计语言》
心情随笔
友情链接
给我留言 (opens new window)
关于我
GitHub (opens new window)
  • JavaSE

    • Java_SE之Java_SE平台与JDK
    • Java_SE_第二讲:原生数据类型
    • Java_SE_第三讲:原生数据类型使用陷阱
    • Java_SE_第四讲:运算符
    • Java_SE_第五讲:运算符续
    • Java_SE_第六讲:流程控制语句
    • Java_SE_第七讲:流程控制续
    • Java_SE_第八讲:理解面向对象程序设计
    • Java_SE_第九讲:面向对象之封装
    • Java_SE_第十讲:面向对象之封装续
    • Java_SE_第十一讲:面向对象之封装续二
    • Java_SE_Lesson_1:面向对象高级
    • Java_SE_Lesson_2:多态与static和final关键字
    • Java_SE_Lesson_3:接口、单例模式、包与访问控制
    • Java_SE之Object类详解
      • 相等性的比较(==)
      • java.lang.Object 类
      • API(Application Programming Interface)
      • toString
      • 进制的表示
      • equals
    • Java_SE之String类及其源代码剖析
    • 包装类与数组
    • 冒泡排序、交换排序与快速排序
    • Java数组的查找方式及二分查找
    • 常量与Java集合框架简介
    • 常用的Java开发IDE
    • ArrayList深入分析
    • LinkedList源代码深入剖析
    • 数据结构中的基本结构分析
    • 《Java语言新特性》

  • 开源框架

  • Linux

  • Struts2

  • Hibernate

  • Webservice

  • 分布式

  • 分布式框架

  • 后端开发
  • JavaSE
terwer
2022-10-12
目录

Java_SE之Object类详解

# 相等性的比较(==)

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

# 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";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

结果:

java.lang.Object@2c8d66b2
java.lang.Object@2c8d66b2
aaa
aaa
Hello World
Hello World
1
2
3
4
5
6

# 进制的表示

关于进制的表示:

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
1
2
3
4
5
6
7
8

# 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 方法,而不要使用 == 。

‍

编辑 (opens new window)
#引用#进制#比较#左右#两边#对象
上次更新: 2023/02/22, 13:47:25
Java_SE_Lesson_3:接口、单例模式、包与访问控制
Java_SE之String类及其源代码剖析

← Java_SE_Lesson_3:接口、单例模式、包与访问控制 Java_SE之String类及其源代码剖析→

最近更新
01
解决css部分border被圆角切掉之后圆角的边框消失问题
03-18
02
使用TypeScript开发一个自定义的Node-js前端开发脚手架
03-08
03
Github-Actions使用release-please实现自动发版
03-06
更多文章>
Theme by Vdoing | Copyright © 2011-2023 Terwer Green | MIT License | 粤ICP备2022020721号-1 | 百度统计
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式