Java Object class introduction, javaobject class
1. The Object class is the base class of all java classes.
If you do not use the extends keyword to specify its base class in the class declaration, the default base class is the Object class, ex:
Public class Person {
~~~~~
}
Equivalent
Public class Person extends Object {
~~~~~
}
2. equals method of Object Class
① The Object class is defined as follows:
Public boolean equals (Object obj) method.
Provides the logic to define whether objects are equal.
② The equals method of Objec is defined as: x. equals (y). If x and y are applications of the same object, true is returned; otherwise, false is returned.
③ Some classes provided by J2SDK, such as String and Date, overwrite the equals () method of the Object and call the equals method of these classes, x. equals (y). If x and y reference the same class object and the attribute content is equal (not necessarily equal), true is returned; otherwise false is returned.
④ The equals () method can be rewritten in the User-Defined type as needed.
public class TestEquals{ public static void main (String args[]){ Cat cat1 = new Cat(1,2,3); Cat cat2 = new Cat(1,2,3); System.out.println(cat1 == cat2); System.out.println(cat1.equals(cat2 )); String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); }}class Cat{ int color; int height,weight; Cat(int color , int height , int weight){ this.color= color; this.height = height; this.weight = weight; } public boolean equals(Object obj){ if(obj == null) return false; else{ if( obj instanceof Cat){ Cat c = (Cat)obj; if(c.color == this.color && c.height == this.height && c.weight == this.weight){ return true; } } } return false; }}
Running result:
Java and java Object types
The upstairs is too detailed! Are you confused ??
Differences between java and java Objec types:
All JAVA classes inherit the word class Object (that is, the package java. lang. object) by default, including the class you created.
Boolean, Integer, Long, and Double are classes in the java. util package. They are boolean, int, long, and double.
After the basic type is called, it is stored in the stac (stack) k after creation, and the access speed is very fast, while other referenced objects are stored in the heap (stack) after creation, so the speed is not as fast as that of the stack. Different creation methods:
Int I = 1; // This creates an int object of the basic type.
Integer integer = new Integer (); // This creates a reference to the Integer packaging class, which has different locations in the memory.
Similarly, there are char and Character, float and Float, short and Short, byte and Byte. Remember that the former is used to generate object references, while the latter directly generates a basic type of data.
There are several methods in the Object class in JAVA.
1. All methods:
1. getClass ()
2. hashCode ()
3. equals ()
4. toString ()
5. clone ()
6. wait ()...
7. notify ()
8. policyall ()
9. finalize ()
2. Functions of each method:
Method Summary
Protected Object clone () creates and returns a copy of this Object.
Boolean equals (Object obj) indicates whether another Object is "equal" to this Object ".
Protected void finalize () when the Garbage Collector determines that there is no more reference to this object, the object's garbage collector calls this method.
Class <? ExtendsObject> getClass () returns the runtime class of an object.
Int hashCode () returns the hash code value of this object.
Void y () Wake up a single thread waiting on this object monitor.
Void policyall () Wake up all threads waiting on this object monitor.
String toString () returns the String representation of the object.
Void wait () causes the current thread to wait until other threads call the notify () or yyall () method of this object.
Void wait (long timeout) causes the current thread to wait until other threads call the notify () method or notifyAll () method of this object, or exceed the specified time.
Void wait (long timeout, int nanos) causes the current thread to wait until other threads call notify () of this object ()