15、Object类详解

Class Object is the root of the class hierarchy.Every class has Object as a superclass.类Object是类层次结构的根,每个类都有一个Object类作为超类(父类)。也就是说每个类最终都继承了Object类。

1、Object类位于java.lang包下,java.lang包在使用时无需显式导入,编译时由编译器自动帮我们导入 。

Object 类有一个不带参数的构造方法,还有9个方法,分别是protected Object clone()、boolean equals()、protected void finalize()、Class<?>getClass()、int hashCode()、void notify()、void notifyAll()、String toString()、void wait()、void wait(long timeout)、void wait(long timeout,int nanos)

2、当打印引用变量时,实际上会打印出引用所指对象的toString()方法的返回值。因为每个类都直接或者间接地继承自Object,而Object类中定义了toString(),因此每个类都有toString()这个方法。如果是一个字符串变量,如String str = "asa";则打印aaa,说明String类重写了toString()方法。(查询java.lang包下String果然有toString方法)。而如果是定义8种原生数据类型,则没有相应的toString方法,如int i = 9;打印i.toString()编译出错:无法取消引用 int

3、打印为什么是xxx@2e4f3形式:主要是toString()方法的原因:The toString method for class Object returns a string consisting of the name of the class of which the object is an instance,the at-sign character '@',and the unsigned hexadecimal representation of the hash code of  the object.in other words,this method returns a string equal to the value of:

      getClass().getName()+'@' +Integer.toHexString(hashCode())

类Object的toString方法返回一个字符串,这个字符串由实例对象所对应的类的类名字+“@”+这个实例对象的无符号十六进制哈希码组成。换句话说,这个方法返回一个等于如下值的字符串:

       getClass().getName()+'@' +Integer.toHexString(hashCode())

4、Object类的equals()方法: 

程序如下:

public class ObjectTest3
{
       public static void main(String[] args)
 {
        String str = new String("aaa");
        String str2 = new String("aaa");

        System.out.println(str.equals(str2));    //true

        System.out.println(str == str2);        //false   

        String str3 = "aa";
        String str4 = "aa";
        System.out.println(str3.equals(str4));   //true

        Object object = new Object();
        Object object2 = new Object();

        System.out.println(object.equals(object2));    //false
 }

结果为:true,false,true,false

查看Object类的源代码,其equals方法实现代码如下:

   public boolean equals(Object obj) {
              return (this == obj);
    }

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

因为这个原因,对于上面的程序,str.equlas(str2),如果String类没有重写equals()方法,他们一定是false的,因为只要new,就会生成一个新的对象,str和str2应该分别指向不同的对象,str == str2 为false。

于是来研究一下String的equals方法

5、关于String:

String的equals方法源代码:

public boolean equals(Object anObject){

      if (this == anObject) {              //如果自己跟自己比,那一定是相等,返回true

            return true;

      }

      if (anObject instanceof String) {                        //判断传进来的对象是否为String类型对象,如果不是,那就不相等,返回false

                                                                                     //如果传进来的是String类型对象,则判断对象的内容是否相等

            String anotherString = (String)anObjet;

            int n = count;

            if (n == anotherString.count) {                  //判断字符串长度是否相等,不等,直接返回false

                  char v1[] = value;
                  char v2[] = anotherString.value;
                  int i = offset;
                  int j = anotherString.offset;
                  while (n-- != 0) {
                        if (v1[i++] != v2[j++])                       //长度相等,一个一个字符比较
                              return fale;

                  }

                 return true;  

            }

      }

      return false

}

 

对于String类的equals()方法来说,他是判断当前字符串与传进来的字符串的内容是否一致。对于String对象的相等性判断来说,请使用equals方法,而不要使用==。

6、字符串的比较:

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

  System.out.println(object == object2);    //false

  System.out.println("----------------");


  String str = new String("aaa");
  String str2 = new String("aaa");

  System.out.println(str == str2);          //false

  System.out.println("----------------");

  String str3 = "bbb";
  String str4 = "bbb";

  System.out.println(str3 == str4);        //true
  
  System.out.println("----------------");

  String str5 = new String("ccc");
  String str6 = "ccc";

  System.out.println(str5 == str6);       //false

  System.out.println("----------------");

  String s = "hello";
  String s1 = "hel";
  String s2 = "lo";

  System.out.println(s == s1 + s2);        //false

  System.out.println("----------------");

  System.out.println(s == "hel" + "lo");   //true
 }
}
---------------------------------------------------------------------------------------------------------------

The String class represents character strings.All string literals in java programs,such as "abc",are implemented as instance of this class.

Strings are constant;their values can not be changed after they are created.String buffers support mutable strings.Because String objects are immutable they can be shared.

String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享.

String是常量,其对象一旦创建完毕就无法改变。当使用+拼接字符串时,会生成新的String对象,而不是原有的String对象追加内容。

java虚拟机提供String Pool(字符串池)

7、当采用字面值方式赋值时,如String s = “aaa”;处理过程如下:

      1)查找String pool中是否存在“aaa”这个对象,如果不存在,则在String Pool中创建一个“aaa”对象,然后将String Pool中的这个“aaa”对象的地址返回来,赋给引用变量s,这样s会指向Strng pool中的这个“aaa”字符串对象

      2)如果存在,则不创建任何对象,直接将String Pool中的这个“aaa”对象地址返回来,赋给s引用。

8、采用:String s = new String(“aaa”);方式时

      1)首先在String Pool中查找有没有“aaa”这个字符串对象,如果有,则不在String Pool中再去创建“aaa”这个对象了,直接在堆中(heap)中创建一个“aaa”字符串对象,然后将堆中的这个“aaa”对象的地址返回来,赋给s引用,导致s指向了堆中创建的这个“aaa”字符串对象。

      2)如果String Pool中没有,则首先在String Pool中创建一个“aaa”对象,然后再在堆中(heap)创建一个“aaa”对象,然后将堆中的这个“aaa”对象的地址返回来,赋给s引用,导致s指向了堆中所创建的这个“aaa”对象。

      3)这种方式下s都指向堆(heap)中的新创建对象

9、String类的intern()方法:Return a canonical representation for the string object.   返回字符串对象的规范化表示形式

 A pool of strings,initially empty,is maintained privately by the class String.When the intern method is invoked,if the pool already contains a string equal to this String Object as determined by the equals(Object) method,then the string from the pool is returned.Otherwise,this String object is added to the pool and a reference to this String object is returned.

It follows taht for any two strings s and t,s.intern() == t.intern() is true if and only if s.equals(t) is true.

一个字符串池,初始时为空,并且被String类独自维护着。当intern方法被调用时,如果字符串池中存在一个字符串等于这个String对象(判断是否相等使用equals(object),就是字符串池中一个字符串string.equals(object)为true),则返回池中的字符串,否则,此String对象添加到池中。并返回此String对象的引用。

遵循以下规则,对于任意两个字符串s和t,当且仅当s.equals(t)为true时,s.intern() == t.intern()才为true。

 intern()方法的主要目的就是为了确保返回的String对象是来自String Pool中的。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值