why if comparison doesn't work in java

Viewed 83

I am making a Hash Table in java. In searching function, I am doing some comparison in IF statement. but it is not doing any comparison.

here's is some part of my code.


while (table[pos]!=null) {
        if (table[pos]==key) {
            System.out.println("SEARCH "+key+" at INDEX "+home);
            return;
        }
        else {pos=h(home+p(i));
        i++;
        }
    }
    System.out.println("Failed to find "+key+".");
    return;
}

It doesn't work even when table[pos] and key are the same! but I add very simple assigning variable to another one. It work! I don't know why it works. I wanna know it xD

   while (table[pos]!=null) {
        int x = table[pos];
        if (x==key) {
            System.out.println("SEARCH "+key+" at INDEX "+home);
            return;
        }
        else {pos=h(home+p(i));
        i++;
        }
    }
    System.out.println("Failed to find "+key+".");
    return;
}
3 Answers
Related