|
| 1 | +1. Object Class (Default hashCode): |
| 2 | + - Agar override nahi karte to Object class ka default hashCode use hota hai. |
| 3 | + - Ye mostly object ka **memory address** ko ek integer me convert karke deta hai (JVM dependent). |
| 4 | + |
| 5 | + Native code hai (C/C++ level pe), Java me directly visible nahi hota: |
| 6 | + ```java |
| 7 | + public native int hashCode(); |
| 8 | + |
| 9 | +⸻ |
| 10 | + |
| 11 | +2. String Class: |
| 12 | +• String ne apna hashCode() override kiya hai. |
| 13 | +• Formula: har character ko leke ek polynomial rolling hash banata hai (base = 31). |
| 14 | + |
| 15 | +public int hashCode() { |
| 16 | + int h = 0; |
| 17 | + for (int i = 0; i < value.length; i++) { |
| 18 | + h = 31 * h + value[i]; |
| 19 | + } |
| 20 | + return h; |
| 21 | +} |
| 22 | + |
| 23 | +Example: "ABC" |
| 24 | +• h = 0 |
| 25 | +• h = 31*0 + ‘A’(65) = 65 |
| 26 | +• h = 31*65 + ‘B’(66) = 2081 |
| 27 | +• h = 31*2081 + ‘C’(67) = 64478 |
| 28 | + |
| 29 | +⸻ |
| 30 | + |
| 31 | +3. Integer Class: |
| 32 | +• Bahut simple → value khud hi hashCode hoti hai. |
| 33 | + |
| 34 | +public int hashCode() { |
| 35 | + return this.value; |
| 36 | +} |
| 37 | + |
| 38 | +Example: |
| 39 | +• new Integer(100).hashCode() = 100 |
| 40 | + |
| 41 | +⸻ |
| 42 | + |
| 43 | +4. Double Class: |
| 44 | +• Double me binary bits ka hash banta hai. |
| 45 | + |
| 46 | +public int hashCode() { |
| 47 | + long bits = Double.doubleToLongBits(value); |
| 48 | + return (int)(bits ^ (bits >>> 32)); |
| 49 | +} |
| 50 | + |
| 51 | +Matlab: double ko 64-bit binary me badal ke 32-bit int me compress karta hai. |
| 52 | + |
| 53 | +⸻ |
| 54 | + |
| 55 | +5. Custom Class Example: |
| 56 | +• Jab tum apni class banate ho, aise override karte ho: |
| 57 | + |
| 58 | +class Student { |
| 59 | + String name; |
| 60 | + int age; |
| 61 | + |
| 62 | + @Override |
| 63 | + public int hashCode() { |
| 64 | + int result = 17; |
| 65 | + result = 31 * result + age; |
| 66 | + result = 31 * result + (name == null ? 0 : name.hashCode()); |
| 67 | + return result; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +Logic: |
| 72 | +• 17 ek prime seed hai. |
| 73 | +• 31 multiplier use karke fields combine karte hain. |
| 74 | +• String ka khud ka hashCode call hota hai. |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +Quick Demo Code: |
| 79 | + |
| 80 | +public class HashCodeInternalDemo { |
| 81 | + public static void main(String[] args) { |
| 82 | + String str = "ABC"; |
| 83 | + System.out.println("String hashCode: " + str.hashCode()); |
| 84 | + |
| 85 | + Integer num = 100; |
| 86 | + System.out.println("Integer hashCode: " + num.hashCode()); |
| 87 | + |
| 88 | + Double d = 12.34; |
| 89 | + System.out.println("Double hashCode: " + d.hashCode()); |
| 90 | + |
| 91 | + Object obj = new Object(); |
| 92 | + System.out.println("Default Object hashCode: " + obj.hashCode()); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +⸻ |
| 97 | + |
| 98 | +Recap: |
| 99 | +• Default hashCode() (Object class) → native, memory-based. |
| 100 | +• String → polynomial with base 31. |
| 101 | +• Integer → direct value. |
| 102 | +• Double → bit manipulation. |
| 103 | +• Custom class → override with fields + 31 multiplier. |
| 104 | + |
| 105 | +⸻ |
0 commit comments