1.网上面试题
这是一道Java面试题。看了非常多答案都说不正确。能够看下面代码。就知道结果了
http://www.iteye.com/topic/485046第45题 答案是错误的
package com.test; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Test x=new Test("x"); Test y=new Test("y"); System.out.println("x HashCode="+x.hashCode()); System.out.println("y HashCode="+y.hashCode()); System.out.println(x.equals(y)==true); } } class Test{ private String name; public Test(String name){ this.name=name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ?0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { return true; } }
2.结果
x HashCode=151 y HashCode=152 true
所以这个面试题是能够的