Several methods of the object class in Java

Source: Internet
Author: User

The object class is called the God class, also known as the Ancestor class. When you define a Java class, if you do not specify a parent class, the object class is inherited by default. With Java's up-type conversion, you can do a lot of work with the object class.

In the object class, there are several common methods, such as GetClass (), toString (), and Equals () three methods. Their implementation in the Object.java is as follows: (Object.java source code in the Java installation directory of Src.zip, its directory structure is Src\java\lang\object.java).

1  Public Final nativeClass<?>getclass ();2 3 4  Public Booleanequals (Object obj) {5     return( This==obj);6 }7 8  PublicString toString () {9     returnGetClass (). GetName () + "@" +integer.tohexstring (Hashcode ());Ten}

For GetClass (), it can get classes at run time.

Equals () is typically used to compare, in the object class, it makes a reference comparison, and returns true only if the reference is the same. But this is not the case of comparisons we want, such as the comparison of strings to compare whether each character is the same. The Equals () method is overridden in the string class, and the Equals () method in the string class is implemented as follows: (the String.java source is in the Java installation directory of Src.zip, its directory structure is src\java\lang\ String.java).

1  Public Booleanequals (Object anobject) {2     if( This==anobject) {3         return true;4     }5     if(AnObjectinstanceofString) {6String anotherstring =(String) anobject;7         intn =value.length;8         if(n = =anotherString.value.length) {9             CharV1[] =value;Ten             CharV2[] =Anotherstring.value; One             inti = 0; A              while(n--! = 0) { -                 if(V1[i]! =V2[i]) -                     return false/; thei++; -             } -             return true; -         } +     } -     return false; +}

A comparison of the strings, made several judgments:

1, first judge the same reference, if the same return true;

2, judge whether the incoming object is a string, if it is not a string, then directly return false, there is no comparison of the necessary, how dogs and people than;

3, compare the length of the string, if the length of the two strings are not the same, you do not have to compare, the length of the different obviously is not the same;

4, the above comparison is not established, then the comparison of the character-by-word.

For a comparison of two objects, we usually need to resemble the Equals () method in the String class, which requires overriding the Equals () method.

The third method, ToString (), defaults to outputting the current run-time class and a hashcode in the object class, which is not particularly large for our help, and for classes that you write, you can output all the properties in the class, and when the ToString () method is overridden, You can call the object's ToString () method by default by using the object name in System.out.println () directly.

As a beginner, is to continue to write code, there is a lot of records of their own learning content, so that they deepen the impression, there is a time to forget the convenient search, record here at any time to facilitate their own!

Several methods of the object class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.