# help with simple programming question

• 05-18-2011, 05:46 PM
eezilyBefuddled
help with simple programming question
I hope this question isn't too simple or dumb...

Code:

```int x = 2; int y = "WKD".hashCode() % 3000; int z = "WWI".hashCode() % 3000; for (int i = 0; i <= z; i++)   a = (x ^ i) % y; return z;```
Can someone please tell me what value is returned for z? Thanks!
• 05-18-2011, 05:54 PM
Dark
Why don't you use System.out.println(z); and find out?
• 05-18-2011, 06:12 PM
eezilyBefuddled
I was hoping that someone here could tell me the (correct) answer... :)
• 05-18-2011, 06:21 PM
JosAH
Quote:

Originally Posted by eezilyBefuddled
I was hoping that someone here could tell me the (correct) answer... :)

It all depends on what the hasCode() method in the String class returns; here it is:

Code:

```    public int hashCode() {         int h = hash;         if (h == 0) {             int off = offset;             char val[] = value;             int len = count;             for (int i = 0; i < len; i++) {                 h = 31*h + val[off++];             }             hash = h;         }         return h;     }```
b.t.w. you can find this in the src.zip file stored in your JDK directory.

kind regards,

Jos
• 05-18-2011, 06:28 PM
Tolls
You think we keep the value of something like:
"WWI".hashCode()

Well, maybe Jos does, but he's strange.
• 05-18-2011, 06:32 PM
JosAH
Quote:

Originally Posted by Tolls
You think we keep the value of something like:
"WWI".hashCode()

Well, maybe Jos does, but he's strange.

kindest regards,

Jos ;-)
• 05-18-2011, 06:34 PM
Dark
You were asking for a handout, I told you how to figure it out.
• 05-18-2011, 06:36 PM
Tolls
Quote:

Originally Posted by JosAH

kindest regards,

Jos ;-)

You realise that forced me to go and check, don't you?
You swine!
• 05-18-2011, 06:53 PM
eezilyBefuddled
Quote:

Originally Posted by Dark
You were asking for a handout, I told you how to figure it out.

Sound like something my dad would say... of course, he was usually right. :eek:
• 05-18-2011, 08:05 PM
JosAH
Quote:

Originally Posted by Tolls
You swine!