private static native void registerNatives();
static {
registerNatives();
}
该方法还在System等其他地方有使用
blog.csdn.net/chenyi8888/article/details/7070367
getClass();
hashCode()
the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects.
为每个不同的对象返回一个不同的哈希值??hash可以相同吗?????
equals
public boolean equals(Object obj) {
return (this == obj);
}
protected native Object clone() throws CloneNotSupportedException;
链接--深克隆,浅克隆
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
源代码
public final void wait() throws InterruptedException {
wait(0);
}
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos > 0) {
timeout++;
}
wait(timeout);
}
public final native void wait(long timeout) throws InterruptedException;
public final native void notify();
public final native void notifyAll();
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
} public boolean equals(Object obj) {
return (this == obj);
}
public native int hashCode();
public final native Class<?> getClass();
//绝大多数情况下这里都已经被改写
public boolean equals(Object obj) {
return (this == obj);
}
改写equals的例子(从中可以看出==和equals的区别,==表示堆内存比较,equals表示根据重写逻辑判断是一致的,这就是为什么对象比较需要使用equals的原因)
package springstudy;
public class ObjectStudy {
public static void main(final String[] args) {
// 在没有重写equals的情况下,equals和==都表示存放在堆中的内存对象
final Object a = new Object();
final Object a1 = new Object();
System.out.println(a.equals(a1));// 没有重写,两者都表示堆中数据,相同为true
System.out.println(a == a1);// true
final Object c = a;// 传引用,两者仍然表示堆中同一个对象
System.out.println(c.equals(a));// true
System.out.println(c == a);// true
//提示我们需要判断某个对象是否是传引用的时候,直接a==b,如果为true则为传引用,否则不是
final B b1 = new B();
final B b2 = new B();
System.out.println(b1.equals(b2));// 根据equals重写,true
System.out.println(b1 == b2);// false,不是堆中同一个对象
}
}
// 看一个例子(为什么equals和“==”不同,在最开始的实现中他们其实是相同的)
class B {
private String aString;
private String bString;
//重写equals
@Override
public boolean equals(final Object obj) {
if (this == obj) {//当内存中相同
return true;
}
//其他条件判断
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
final B other = (B) obj;
if (this.aString == null) {
if (other.aString != null) {
return false;
}
}
else if (!this.aString.equals(other.aString)) {
return false;
}
if (this.bString == null) {
if (other.bString != null) {
return false;
}
}
else if (!this.bString.equals(other.bString)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.aString == null) ? 0 : this.aString.hashCode());
result = prime * result + ((this.bString == null) ? 0 : this.bString.hashCode());
return result;
}
}
一个生产者消费者模型,其实代码注释里面就有提到进行如下实现
package com.wang.study;
import java.util.Queue;
import java.util.Random;
import com.google.common.collect.Queues;
public class ObjectStudy {
Random random = new Random();
public static void main(String[] args) {
Queue<Integer> queue = Queues.newConcurrentLinkedQueue();
int maxSize = 10;
new ObjectStudy(queue, maxSize);
}
public ObjectStudy(final Queue<Integer> queue, final int maxSize) {
Producer producer = new Producer(queue, maxSize);
Consumer consumer = new Consumer(queue);
Thread producerThread = new Thread(producer);
Thread consumerThread = new Thread(consumer);
producerThread.start();
consumerThread.start();
}
private class Consumer implements Runnable {
private Queue<Integer> queue;
public Consumer(final Queue<Integer> queue) {
this.queue = queue;
}
public void run() {
while (true) {
synchronized (queue) {
if (queue.size() == 0) {
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Integer value = queue.poll();
System.out.println(getClass().getName() + "------" + value);
queue.notify();
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
private class Producer implements Runnable {
private Queue<Integer> queue;
private int maxSize;
private int hello = 0;
public Producer(final Queue<Integer> queue, final int maxSize) {
this.queue = queue;
this.maxSize = maxSize;
}
public void run() {
while (true) {
synchronized (queue) {
if (queue.size() == maxSize) {
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
hello++;
queue.add(hello);
System.out.println(getClass().getName() + "------" + hello);
queue.notify();
try {
Thread.sleep(random.nextInt(1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}