Detailed java object-oriented class 16_Object

1.Object class presentation

What is the Object class? Object classes are stored in the java.lang package, all the java class (Object Class excluding) the ultimate parent. Of course, the array also inherited the Object class. If the class does not specify its base extends keyword in the class declaration, the default base class for the Object class.

Object class as follows:
Here Insert Picture Description
any Java class inherit these methods, and is not modified final method can be overridden. For example: no final modified toString () function can be covered, but the wait final () function can not.

Object class method briefly:

public String toString () Returns a string representation of the object.

public Boolean the equals (Object obj) determines whether the address of the same two objects.

public Native int the hashCode () Returns the hash code value for this object (a collection of talk).

public Final Native Class <?> getClass () to obtain configuration information (reflecting speaking), or a class of objects.

public final void wait() throws InterruptedException;

public final native void wait(long timeout) throws InterruptedException;

public final void wait(long timeout, int nanos) throws InterruptedException;

The role of the above three methods: wait, notify or notifyAll need to wake up (talk about multi-threading)

public final native void notify();

public final native void notifyAll();

The role of these two methods: Wake (multi-threaded speaking)

native keyword explanation:

representatives of local native methods, all with native methods are not modified method body. Java is not perfect, Java's lack addition to reflect on than traditional C ++ is much slower than the speed, Java can not directly access to the underlying operating system (such as hardware, etc.), for Java to use native methods to expand the Java program Features. The native method can be compared to Java program interfaces with the C program, use of native modified methods to access the underlying operating system.

2.toString method

toString method returns a string representation of the object, in fact, the string content is: + @ + type hash code of the object.

[Example] The method of the underlying code for toString

public String toString() {
     return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

As a result toString method returns the memory address, and in development, often we need to give the corresponding string representation according to properties of the object, and therefore it needs to be rewritten.

[Example] rewrite toString method Case

class TestDemo {
	String name;
	int age;
	public TestDemo(String name, int age) {
		this.name = name;
		this.age = age;
	}
	// 重写toString方法
	@Override
	public String toString() {
		return "TestDemo[name:" + name + " age:" + age + "]";
	}
}
public class ToStringDemo {
	public static void main(String[] args) {
		TestDemo demo = new TestDemo("小明", 18);
		// 输出:TestDemo[name:小明 age:18] 
		System.out.println(demo);
	}
}

3.equals method

Object class equals method for comparing two objects are the same, it is actually the memory address used in the comparison of two objects. equals method of Object class is the == comparison operators used internally.

[Example] equals method to achieve the underlying code

public boolean equals(Object obj) {
     return (this == obj);
}

[Example] equals method of use cases

// Demo类
class Demo {
	String name;
	int age;
	public Demo(String name, int age) {
		this.name = name;
		this.age = age;
	}
}
// 测试类
public class EqualsDemo {
	public static void main(String[] args) {
		Demo d1 = new Demo("小明", 18);
		Demo d2 = new Demo("小明", 18);
		// d1和d2是不同的两个对象,那么在堆中的地址肯定不相同
		System.out.println(d1 == d2);  // 返回:false
		System.out.println(d1.equals(d2)); // 返回:false
		// 此时,d3和d1都指向堆中同一块地址
		Demo d3 = d1;
		System.out.println(d1.equals(d3)); // 返回:true
	}
}

In development to compare whether two objects are the same, often compared according to the attributes of the object, which is often necessary in developing subclass overrides the equals method are compared according to the attributes of the object.

[Example] determines the attribute values ​​are the same class Demo

// Demo类
class Demo {
	String name;
	int age;
	public Demo(String name, int age) {
		this.name = name;
		this.age = age;
	}
	/**
	 * 重写equals方法,判断属性是否全部相同
	 */
	@Override
	public boolean equals(Object obj) {
		// 1.判断传入的obj是否为null,为null则返回false
		if(obj == null) {
			return false;
		}
		// 2.判断地址是否相同,相同则返回true,否则继续判断属性是否相同
		if(this == obj) {
			return true;
		}
		// 3.依次判断对象中属性值是否相同,相同则返回true,否则返回false
		Parent p = (Parent)obj; // 强转
		if(this.name.equals(p.name) && this.age == p.age) {
			return true;
		}
		return false;	
}
}
// 测试类
public class EqualsDemo {
	public static void main(String[] args) {
		Demo d1 = new Demo("小明", 18);
		Demo d2 = new Demo("小明", 18);
		// 判断属性是否全部相同
		System.out.println(d1.equals(d2)); // 返回:true
	}
}

4.hashCode method

hashCode method Returns the hash code value for this object, the method is to provide some support for the hash table advantages, e.g., the hash table java.util.Hashtable provided.

We direct the output of an object, then the hexadecimal code after the @ is a hash value.

[Example] using the method hashCode Case

Person p = new Person();
System.out.println(p.hashCode()); // 输出:2018699554
System.out.println(p); // 输出:com.bjsxt.objectClass.Person@7852e922
// Integer.toHexString()方法是把哈希值转化为16进制
System.out.println(Integer.toHexString(p.hashCode())); // 输出:7852e922

Because the hashCode method does not use final modification, all we can rewrite the hashCode method. General rewrite the equals method should override the hashCode method. If the result obj1.equals (obj2) is true, then you can launch hashCode obj1 obj2 objects and objects certainly equal, but equal does not necessarily meet hashCode equals. However, in order to improve efficiency, we should try to make the above two conditions are nearly equivalent.

ps: For the latest free documentation and instructional videos, please add QQ group (627,407,545) receive.

Published 55 original articles · won praise 0 · Views 792

Guess you like

Origin blog.csdn.net/zhoujunfeng121/article/details/104600445