Oo object class for Java

Source: Internet
Author: User

Java is the language of redemption, the core idea: to find the right thing for the right object

Method One: Customize the class and then create the object from the custom class

Way Two: Sun provides a lot of classes for us to use, we just need to know these classes, we can create objects through these classes

The object class is the ultimate parent class for all classes

Common methods of the object class:

ToString (); Returns the string representation of the object, the question: what does ToString () do, and after overriding ToString, when we output an object directly, we output the appropriate data that meets our needs

Equals (object obj) is used to compare whether the memory address of two objects is the same object and whether the two objects are the same object

Hashcode () returns the hash of the object (refers to the memory address of the object)

Specifications in Java: Generally we rewrite the Equals method of a class, and we'll rewrite his hashcode method.

How to View source code

Mode one: Hold down the CTRL key, the source code you need to see

Mode two: You can press F3 to move the cursor to the code that needs to be viewed

Why we need to see the source code

1. Viewing the source code can give us a deeper understanding of how others have written this technology.

2. Receive the thoughts of others

See the source code the most taboo thing, not every line of code can be read, to guess the line of code requirements

package com.yuanzijian01;class person{int id; String name;public person (Int id, string name) {this.id = id;this.name  = name;} Currently we need to output an object at the time and the output object's ToString method returns the string public string tostring () {return  "Number:"  +  this.id +  ", Name:"  + this.name;} Public boolean equals (object obj) {person p =  (person) obj;return this.id  == p.id;} Public int hashcode () {return this.id;}} Public class demo01 {public static void main (String[] args)  {//  todo  Auto-generated method stub/*demo01 d = new demo01 (); System.out.println (D.tostring ());         system.out.println (d);      */person p1 = new person ("Yuanzijian"); System.out.println (p1); Person p2 = new person ("Yaunzijian"); System.out.println (p2); System. OUT.PRINTLN (P1.equals (p2)); System.out.println ("P1:"  + p1.hashcode ()); System.out.println ("P2:"  + p2.hashcode ());}}

Execution results

Item No: 110, Name: Yuanzijian ID: 110, Name: yaunzijiantruep1:110p2:110


Oo object class for 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.