由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问个java hashcode的题
相关主题
请教一个 Set 的Java面试题Google 电面
问个常见算法题的变形[面试题求教]remove common phrases from each sentence
问个java List的问题也发面经
一道面试题(integer to binary string)leetcode 上 wordladderII 求教
问一个facebook的电面贡献G家电面面经
好不容易写了个bug free, 可是被说会秒据, 帮看看Word ladder II 感觉算法已经是最优了,但是过不了大测试,能不能帮忙看看?
Java的hashcode和equal函数有什么用?java多线程问题请教 (转载)
问下LeetCode上的题目:count and say微软有组在招new grad software engineer吗?
相关话题的讨论汇总
话题: string话题: test话题: object话题: public话题: flag
进入JobHunting版参与讨论
1 (共1页)
r**e
发帖数: 226
1
为什么答案是 3? 不是2么?
package test;
import java.util.HashSet;
public class Test {

private String str;

public Test(String str) {
this.str = str;
}

@Override
public int hashCode() {
int hascode = this.str.hashCode();
return hascode;
}

@Override
public boolean equals(Object obj) {
return this.str.equals(obj);
}

public static void main(String args[]) {
Test h1 = new Test("1");
Test h2 = new Test("1");
String s1 = new String("2");
String s2 = new String("2");

HashSet hs = new HashSet();
boolean flag = hs.add(h1);
flag = hs.add(h2);
flag = hs.add(s1);
flag = hs.add(s2);

System.out.print(hs.size());
}
}
r**e
发帖数: 226
2
i see. the equals method is wrong...
c******r
发帖数: 5
3
Correct me if I am wrong.
String objects are immutable- which means they physically cannot be changed.
Therefore, no matter how many instance you create (with the same content),
they all "reference" to the same object in the heap.
g**e
发帖数: 6127
4
wrong. Strings are immutable, but they may not refer to the same object.
String a = "abcd";
String b = new String("abcd");
String c = "ab" + "cd";
String tmp = "ab";
String d = tmp + "cd";
is a==b==c==d?
this is a very typical interview question.
Also make sure you know the concept of String Literal, as well as what
String.intern() does.
Also know what is StringBuffer and StringBuilder, what are their differences.
Then you won't have any problem answering string interview questions in Java.

changed.
,

【在 c******r 的大作中提到】
: Correct me if I am wrong.
: String objects are immutable- which means they physically cannot be changed.
: Therefore, no matter how many instance you create (with the same content),
: they all "reference" to the same object in the heap.

g**e
发帖数: 6127
5
我觉得版主应该给我个包子,写了这么多

differences.
Java.
content)

【在 g**e 的大作中提到】
: wrong. Strings are immutable, but they may not refer to the same object.
: String a = "abcd";
: String b = new String("abcd");
: String c = "ab" + "cd";
: String tmp = "ab";
: String d = tmp + "cd";
: is a==b==c==d?
: this is a very typical interview question.
: Also make sure you know the concept of String Literal, as well as what
: String.intern() does.

c******n
发帖数: 710
6
mark
x***i
发帖数: 585
7
equals 方法调用的不是string的equals而是父类object的equals。所以test1和test2
不相等。
J**F
发帖数: 144
8
h1 and h2 are interned
3 is correct
1 (共1页)
进入JobHunting版参与讨论
相关主题
微软有组在招new grad software engineer吗?问一个facebook的电面
问个string combination的问题好不容易写了个bug free, 可是被说会秒据, 帮看看
M家面试,问java一题Java的hashcode和equal函数有什么用?
请教一个 Java hashcode 和 equals 的面试题!问下LeetCode上的题目:count and say
请教一个 Set 的Java面试题Google 电面
问个常见算法题的变形[面试题求教]remove common phrases from each sentence
问个java List的问题也发面经
一道面试题(integer to binary string)leetcode 上 wordladderII 求教
相关话题的讨论汇总
话题: string话题: test话题: object话题: public话题: flag