Write six commonly used methods of class java.lang.Object

java language is object-oriented, and the java Object class for all classes in the top-level parent (root class).

Each class uses the Object class as super class, all objects (including arrays) are implementations of this class, even if a class does not use extends clear that inherits from a class, it will by default inherit the Object class.

Object class provides a number of ways, just to take one of the more common methods to do the next briefly.

1) public String toString ()  >>> Get Object method information

This method is called when printing an object, the object becomes the information returned string, the default output target address.

for example:

/ ** 
* define a class and override toString () method
* /
public
class the Person { Private String name; Private int Age; Private char Gender; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender=" + gender + '}'; } public Person() { } public Person(String name, int age, char gender) { this.name = name; this.age = age; this.gender = gender; }
/**
 * Test class
 */
public class Test {
  public static void main(String[] args) {
    P1 the Person = new new the Person ( "Joe Smith", 22, 'M' );
    P2 the Person = new new the Person ( "Li Si", 23, 'F' );
    System.out.println(p1.toString());
    System.out.println(p2.toString());
  }
}

The result: consistent with the rewrite toString () result structure.

2) public Boolean the equals (Object obj) >>> objects are equal Analyzing Method

This method is used if the comparison is equal, and this method must be overridden.

for example:

/**
 * Definition of a class and a method override equals
 */
public class Person {
  private String name;
  private int age;
  private char gender;
  public Person() {
  }
  public Person(String name, int age, char gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public char getGender() {
    return gender;
  }
  public void setGender(char gender) {
    this.gender = gender;
  }
  /**
   * Override equals method
   */
  public boolean equals(Person p) {
    return super.equals(p);
  }
}
/**
 * Test class
 */
public class Test {
  public static void main(String[] args) {
    P1 the Person = new new the Person ( "Joe Smith", 22, 'M' );
    P2 the Person = new new the Person ( "Joe Smith", 23, 'M' );
    P3 the Person = new new the Person ( "Joe Smith", 22, 'M' );
    Integer a = 10;
    Integer b = 10;
    boolean flag1 = p1.equals(p2);
    boolean flag2 = p1.equals(p3);
    boolean flag3 = p1.getName().equals(p2.getName());
    boolean flag4 = a.equals(b);
    System.out.println(flag1);
    System.out.println(flag2);
    System.out.println(flag3);
    System.out.println(flag4);
  }
}

Operating results: equals comparison is the address, even though p1 and p3 various attributes of the same, but by creating two objects in the heap address value is not the same, so the result is false.

Note that the data type is not substantially equals method. The above examples are being given int into Integer, p1.getGender (). Equals (p2.getGenderis wrong.

3) public Native int hashCode () >>> object signature

This method is used to return to his physical addresses (hash code value) of an object, and often equals method to rewrite data, ensuring equal two objects have equal .hashCode.

  /**
   * Override the hashCode method
   */
  @Override
  public int hashCode() {
    return super.hashCode();
  }
/**
 * 测试类
 */
public class Test {
  public static void main(String[] args) {
    Person p1 = new Person("张三",22,'男');
    Person p2 = new Person("张三",22,'男');
    int i1 = p1.getName().hashCode();
    int i2 = p2.getName().hashCode();
    int i3 = p1.hashCode();
    int i4 = p2.hashCode();
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3);
    System.out.println(i4);
  }
}

运行结果:

由此可见通过new的对象即使属性赋值一致,对象的hashCode也是不一样的。

4)public final native Class<?> getClass() >>> 返回此Object的运行时类

注意:此方法不可重写,要调用的话一般和getName()联合使用,如getClass().getName()

该方法在反射中有大用!

5)线程中常用的方法(此处合在一起说明)

public final void wait()  >>> 多线程中等待功能

public final native void notify() >>> 多线程中唤醒功能

public final native void notifyAll() >>> 多线程中唤醒所有等待线程的功能

其中wait()方法还有另外两个带不同功能的方法:

public final native void wait(long timeout) >>> 要等待的最长时间,单位毫秒

public final void wait(long timeout, int nanos) >>> nanos额外时间,单位纳秒

6)protected native Object clone() >>> protected修饰的方法,功能是完成对象的浅复制(创建并返回此对象的一个副本----“副本”的准确含义可能依赖于对象的类)

只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException。

Java中除了8种基本类型传参数是值传递,其他的类对象传参数都是引用传递,我们有时候不希望在方法里将参数改变,这时就需要在类中复写clone方法(实现深复制)。

7)protected void finalize() >>> 垃圾回收(释放资源的方法)

该方法即使被调用也不知道什么时候执行,所以一般很少主动使用。

finalize()方法的用途:

无论对象是如何创建的,垃圾回收器都会负责释放对象占据的所有内存。这就将对finalize()的需求限制到一种特殊情况,即通过某种创建对象的方式以外的方式为对象分配了存储空间。这种情况一般发生在使用“本地方法”的情况下,本地方法是一种在Java中调用非Java代码的方式。

*>>Java的一大特点就是垃圾回收机制。关于垃圾回收需要注意以下几点:

1)对象可能不被垃圾回收。

只要程序没有濒临存储空间用完的那一刻,对象占用的空间就总也得不到释放。

2)垃圾回收并不等于“析构”。

3)垃圾回收只与内存有关。

使用垃圾回收的唯一原因就是为了回收程序不再使用的内存。

综上所述,Object类常用的六种方法是toString()equals()hashCode()wait()notify()notifyAll()

Guess you like

Origin www.cnblogs.com/sinoaccer/p/12043631.html