Java中常用的类—Object类

Object类

1、equals()方法。

<1>比较两个引用是否指向同一个对象。
<2>被比较的两个引用所指向的对象的状态(或属性)是否相同。

注:==在比较String对象时比较的是同一对象才为true,而equals是在比较同一对象、或具有相同状态的两个对像时都为真。

用法:

String str1="hello";
String str2="hello";
String str3=new String("hello");
System.out.println(str1.equals(str2));//true
System.out.println(str1.equals(str3));//true

2、hashCode()方法。
主要就是由对象导出一个hashCode值。hashCode值由对象的状态决定,只要对象的状态相同即对象的hashCode值就相同。

用法:

String str1="hello";
String str2=new String("hello");
String str3="world";
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
System.out.println(str3.hashCode());

//注:str1和str2的对象不是同一个对象,但是它们两
//个对象的状态(即属性)都是相同的,因此str1
//和str2的hashCode值是相等的。有由于str3对象的状
//态与str1、str2不同,因此str1、str2与str3的
//hashCode值是不同的。

3、toString()方法
将对象转换成有实际意义的字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值