Java Learning Lesson 17th (Application of object and some of its methods)

Source: Internet
Author: User

Object: Root class for all classes

object is constantly extracted and has a relationship with all objects

Method Summary:

Clone (): Creates and returns a copy of this object equals (Object obj): Indicates whether other objects are "equal" to this object Finalize (): When the garbage collector determines that there are no more references to the object, This method is called by the object's garbage collector GetClass (): Returns the Objext runtime Class Hashcode (): Returns the hash code value of the object Notify (): Wakes up a single thread waiting on this object monitor Notifyall (): Wakes up all threads waiting on this object monitor Tostrin G (): Returns the object's string representation of Wait (): Causes the current thread to wait until the other thread calls the Notify () or Notifyall () method of this object (long timeout): The Notify () or Notifyall () method of this object is called by another thread, or the current thread waits for wait (long timeout,int before the specified amount of time Nanos): Causes the current thread to wait until another thread calls this object's notify () or Notifyall () method, or if another thread interrupts the current thread, or has exceeded an actual amount of time

One, Equals (): (Key to Master)

class Man{private int age = 3; Man (int.) {this.age = age;}} Class Son{}public class Main{public static void Main (string[] args) {Man BLF = new Mans (20); Man BLF1 = new Mans (20); Man BLF2 = BLF1; System.out.println (BLF==BLF1);//falsesystem.out.println (Blf.equals (BLF1));//flase//Why is it false? Once a man (20) passes the age and the other man ("xxx") passes the name, these two certainly are different System.out.println (Blf1.equals (BLF2));//trueson BLF5 = new Son (); System.out.print (Blf2.equals (BLF5));//A different class-defined object can also be compared}}

Of course, most of the time, equals only judge the address is not enough, can be replicated
This method is generally overridden to establish the basis for determining whether an object is equal based on the object's unique content.

Import Java.net.interfaceaddress;import Javax.management.runtimeerrorexception;class man extends Object{private int Age = 3; Man (int.) {this.age = age;} Compare whether the same age public boolean CMP (man B) {return this.age==b.age;} But man inherits from Objext,objext there is the Equals method, there is no need to write CMP this repeating method//Replication Parent class Equalspublic Boolean equals (Object B)//polymorphic, up transformation {if (! ( b instanceof man) {//return false;//or warning type error throw new RuntimeException ("Do not deliberately pick a fault, only judge people");} Man C = (man) b;//down transformation return this.age==c.age;}} Class Animol{}public class Main{public static void Main (string[] args) {Man BLF = new Mans (20); Man BLF2 = new Mans (20); System.out.println (blf.cmp (BLF2)); Animol BLF3 = new Animol (); System.out.println (Blf.equals (BLF3));}}



Second, hashcode ()//hash, important application in the set frame

Import Java.net.interfaceaddress;import Javax.management.runtimeerrorexception;class man extends Object{private int Age = 3; Man (int.) {this.age = age;}} Class Animol{}public class Main{public static void Main (string[] args) {Man BLF = new Mans (20); Man BLF2 = new Mans (20); System.out.println (BLF); System.out.println (Blf.hashcode ());//Decimal System.out.println (integer.tohexstring (Blf.hashcode ()));}}

In real-world development, you may want to define the hash value of the object yourself, so the hashcode needs to be replicated

Import Java.net.interfaceaddress;import Javax.management.runtimeerrorexception;class man extends Object{private int Age = 3; Man (int.) {this.age = age;} public int hashcode () {return age;}} public class Main{public static void Main (string[] args) {mans BLF = new Man (20); Man BLF2 = new Mans (20); System.out.println (BLF); System.out.println (Blf.hashcode ());//decimal, 20 hexadecimal for 14system.out.println (integer.tohexstring (Blf.hashcode ()));}}



Iii. Methods of GetClass

When creating an object, load the Man.class bytecode file object in memory, then New Man (); Each object has its own byte-code file object

Import Java.net.interfaceaddress;import javax.management.runtimeerrorexception;class man {private int = 3; Man (int.) {this.age = age;} public int hashcode () {return age;}} public class Main{public static void Main (string[] args) {mans BLF = new Man (20); Man BLF2 = new Mans (20); Class C = Blf.getclass (); Class d = Blf2.getclass (); System.out.println (c); System.out.println (C==d);//true, the byte-code file object of two objects is the same as//class in the current runtime class name GetName (System.out.println ()) obtained by the C.getname method; /man}}


Iv. tostring Method:


Import Java.net.interfaceaddress;import javax.management.runtimeerrorexception;class man {private int = 3; Man (int.) {this.age = age;}} public class Main{public static void Main (string[] args) {mans BLF = new Man (20); Man BLF2 = new Mans (20); System.out.println (BLF);//[email protected],@ was getname,@ later Hashcode System.out.println (blf.tostring ());// In fact, the BLF behind is omitted. ToString () System.out.println (Blf.getclass (). GetName () + "----" +integer.tohexstring (Blf.hashcode ( )));}}

It is recommended that all subclasses override this method

Import Java.net.interfaceaddress;import javax.management.runtimeerrorexception;class man {private int = 3; Man (int.) {this.age = age;} Public String toString () {return ' man ' +age;}} public class Main{public static void Main (string[] args) {mans BLF = new Man (20); Man BLF2 = new Mans (20); System.out.println (BLF);//man20}}

PS: There are some ways to wait until the residue learns to multithreading is to complement the whole

Java Learning Lesson 17th (Application of object and some of its methods)

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.