Summary of common methods for parent object in Java _java

Source: Internet
Author: User
Tags numeric value object object

Brief introduction

Object class: This class java.lang.java is the default inherited parent class for all classes

Three methods commonly used in the object class: toString() , equal() ,hashCode()

Self-description of the object of ToString () method, introduction of the object

In the object's self-describing process, the use of get() methods to print instance variables is cumbersome, for simplicity, use toString() .

Public String toString () {return


"learn" + Getno () + "name" + GetName ();

}

System.out.println(s); is equivalentSystem.out.println(s.toString());

In print:

A. Print their values directly when basic data types

B. Referencing a data type is called when toString() printing

If you do not overwrite the method in the object class toString() , print the object directly, the result of the console is:

Hashcode of package full name @ Object

The Equals () method overrides Equals () to be sure to rewrite hashcode (),

Equals()Compares the contents of two objects, or two objects, the hashCode() internal address of an object, and an integer.

= = is the difference from equals ()?

A: = = The real comparison is binary, the basic data type is compared to the numeric value, reference data type comparison is the address

Equals()Divided into the object class equals() and the String class equals() , the object class equals() equals = = compares the reference type condition; the String class equals() overrides the method in the object class, which equals() compares literal values.

Instanceof to determine the class name, or perhaps use reflection

Object reference variable name instanceof class name This expression, if the object's reference variable name is the class name after the class or the base class object its return value is still true.

Custom equals() methods:

1. Same Type de

2. Define your own comparison rules

public Boolean equals (Object object) {//rewrite the Equals () method of object

equalstest equalstest = (equalstest) object;

if (equalstest.getclass () = = Equalstest.class) {//???????????

if ((this.name==equalstest.name) && (this.price== qualstest.price)) {return

true;

} else{return

false;

}

else{return

false;

}

Third, Hashcode () method to speed up the search, to achieve index lookup (query algorithm, storage algorithm)

public int hashcode () {return

This.name.hashCode () *13 + new Interger (getage). Hashcode () *12;

The two principles of hashcode:

1. When the two object equals() method compares the return value to True, their hashCode() method also guarantees that the return value is the same.

2. Objects used equals() to compare the use of attributes, should be used to calculate hashCode() .

Hashcode () hash algorithm

Demand:

1. hashCode() The return value is the same and the equals() return value must be the same.

2. Rewrite hashCode() What attributes are used, and equals() what attributes to use in the method.

Note: Hashcode is the same, equals are not necessarily the same.

Hashcode is used to judge folders, and equals is used to determine files.

If you do not override the hashCode() method, the default Hashcode () method is based on the address of the object (the key value is the address of the object).

If the hashCode() method is rewritten, it cannot be used to determine whether it is the same object,

You can use System.identityHashCode (the reference name of an object) the same object if the hashcode is the same.

System.out.println(System.identityHashCode(对象的引用名));

If not hashCode() , the object's hashcode is calculated from the object address.

System.out.println(对象引用名.hashCode());

Note: The method of whether an element exists in the HashSet collection and the deletion operation depends on the element's hashCode() andequals()

Summarize

The above is for everyone to organize the Java in the parent object of the three common methods, I hope to be helpful to everyone, if there are questions you can message exchange, thank you for your support cloud habitat community.

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.