The object class in Java describes _java in detail

Source: Internet
Author: User

Theoretically the object class is the parent class of all classes, that is, directly or indirectly inheriting the Java.lang.Object class. Because all classes inherit from the object class, the extends object keyword is omitted.
The main methods in this class are ToString (), GetClass (), Equals (), Clone (), Finalize (), where ToString (), GetClass (), Equals is the most important method.

Attention:

GetClass (), notify (), Notifyall (), wait (), and so on are defined as final types and cannot be overridden in the object class.

GetClass () method;
Can not be rewritten, to invoke words, General and getName () are used jointly, such as GetClass (). GetName ();
ToString () method;
Can be overridden, if a specific output pattern is provided for a particular object in actual use, the overridden ToString () method is built automatically when the type is converted to a string or string connection.

Public objectinstance{public 
String toString () {return 
 "in" +getclass (). GetName () + override ToString () method 
} Public 
static void Main (String arg[]) { 
  System.out.println (new ObjectInstance ()); 
} 

Equals () method;

Class V { 
} public 
class overwriteequals{public 
  static void main (string args[]) { 
    string s1= "123"; 
    String s2= "123"; 
    System.out.println (s1.equals (S2)); 
    V v1= New V (); 
    V v2= New V (); 
    System.out.println (V1.equals (v2)); 
  } 

Output results:

Run: 
true 
false 

As you can see from this example, when you compare using the Equals () method in a custom class, you return false because the default implementation of the Equals method is the "= =" operator, which compares the reference addresses of two objects rather than the contents of the objects. So to really compare the contents of two objects, you need to override the Equals () method in your custom class.

Related Article

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.