Java Core API--1 (object class)

Source: Internet
Author: User


1. Object class

In the Java system, the Java.lang.Object class is at the top (the direct or indirect parent class for all objects). If a class does not declare its parent class without a write extends keyword, the class inherits the Java.lang.Object class by default. The object class defines the basic behavior of "objects", and the quilt class inherits by default.

1) ToString Method: Returns a string that can represent the contents of the property of the object.

MyObject obj=new MyObject (); MyObject class Customization

String info=obj.tostring ();

SYSTEM.OUT.PRINTLN (info);

A The previous example MyObject class has no ToString method, and the object can also call ToString ()?

Since all classes inherit from object, and the ToString method is ojbect defined, we inherit this method directly. The ToString method of object returns a string with a fixed format: type @hashcode, which is a string of numbers, called a handle in Java, or called an address (but not the actual physical address, which is Java's own set of virtual addresses, Prevent direct manipulation of memory).

Public String toString () {//can only be used with public, overriding method access permission is greater than or equal to the method in the parent class

Return "This is our own definition of the ToString method return value myobject!";

}

B Why would you want to override the ToString method?

The intent of the ToString definition is to return a string of text that describes an instance of the current class, and we look at a bunch of hashcode that don't make sense, so we're going to rewrite it.


2) Equals method: The "equality" logic for the object.

A Definition in object:

public boolean equals (Object obj) {

return (this==obj);

}

Thus, the this==obj and the direct = = (double equals) effect, just based on the object's address (handle, that hashcode value) to determine whether the object is equal. Therefore, if you want to compare objects with the content of a given object, you must override the Equals method.

B The difference between "= =" and equals:

When comparing an object with "= =", it describes whether two objects are the same object! Judged by the address value. The Equals method tries to describe whether the contents of two objects are equal, the content is equal depending on the business logic needs, and the comparison rules can be defined by itself.

C Override of the Equals method: for example, determine whether two points are equal.

public boolean equals (Object obj) {

/* Note parameters, if the address of the given object obj is the same as the current object address, then they are the same object,

* There is a lot of content comparison logic in the Equals method, plus this judgment will save the performance cost!

*/

if (this = = obj) {

return true;

}

/* Security verification before equals comparison ensures that the given object is not null! If obj is null,

* If the reference variable does not point to any object, then you cannot refer to the object that the OJB refers to (because the object does not exist)

* The properties and methods! If you do this, it will throw nullpointexception, NULL pointer exception!

*/

if (obj = = null) {

return false;

}

/* There is a risk of directly converting object to subclass! We can't guarantee that object and the objects we're comparing are

* The same type of this will cause classcastexception! We call it: Class modeling anomalies.

* The first thing to do when overriding equals is to determine if the given object is the same type as the current object .

* Not the same type of direct return false, because there is no comparability!

*/

if (! ( obj instanceof point)) {

return false;

}

Is the same type, the conversion

Point p= (point) obj;

//return content comparison (based on actual situation)

return this.x==p.x && this.y==p.y;

}


2. Introduction to the API

API documentation is what we use to understand the class libraries provided in the JDK, we can first enter through the index and find the classes we need to know, and then we can easily understand the role of the class, the role of constants, and the role of all the methods provided by the class, as well as the parameters of the method and the meaning of the return value.

650) this.width=650; "style=" width:663px;height:496px; "title=" 12.jpg "src=" http://s3.51cto.com/wyfs02/M02/6D/A5/ Wkiom1voiaegyv-uaaetdl48mlq587.jpg "width=" 1102 "height=" 638 "alt=" Wkiom1voiaegyv-uaaetdl48mlq587.jpg "/>


This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1656398

Java Core API--1 (object 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.