001package algs34;
002
003import java.util.Arrays;
004import java.util.Objects;
005
006public class XBuiltInHashcodes {
007        public static void main (String[] args) {
008                System.out.format ("int\n");
009                System.out.format ("%x ", Integer.hashCode (0));
010                System.out.format ("%x ", Integer.hashCode (1));
011                System.out.format ("%x ", Integer.hashCode (2));
012                System.out.format ("%x ", Integer.hashCode (Integer.MAX_VALUE));
013                System.out.format ("%x ", Integer.hashCode (-0));
014                System.out.format ("%x ", Integer.hashCode (-1));
015                System.out.format ("%x ", Integer.hashCode (-2));
016                System.out.format ("%x ", Integer.hashCode (Integer.MIN_VALUE));
017                System.out.println ();
018
019                System.out.format ("\nlong\n");
020                System.out.format ("%x ", Long.hashCode (0));
021                System.out.format ("%x ", Long.hashCode (1));
022                System.out.format ("%x ", Long.hashCode (2));
023                System.out.format ("%x ", Long.hashCode (Long.MAX_VALUE));
024                System.out.format ("%x ", Long.hashCode (-0));
025                System.out.format ("%x ", Long.hashCode (-1));
026                System.out.format ("%x ", Long.hashCode (-2));
027                System.out.format ("%x ", Long.hashCode (Long.MIN_VALUE));
028                System.out.println ();
029
030                System.out.format ("\nfloat\n");
031                System.out.format ("%x ", Float.hashCode (0));
032                System.out.format ("%x ", Float.hashCode (1));
033                System.out.format ("%x ", Float.hashCode (2));
034                System.out.format ("%x ", Float.hashCode (Float.MAX_VALUE));
035                System.out.format ("%x ", Float.hashCode (-0));
036                System.out.format ("%x ", Float.hashCode (-1));
037                System.out.format ("%x ", Float.hashCode (-2));
038                System.out.format ("%x ", Float.hashCode (Float.MIN_VALUE));
039                System.out.println ();
040
041                System.out.format ("\ndouble\n");
042                System.out.format ("%x ", Double.hashCode (0));
043                System.out.format ("%x ", Double.hashCode (1));
044                System.out.format ("%x ", Double.hashCode (2));
045                System.out.format ("%x ", Double.hashCode (Double.MAX_VALUE));
046                System.out.format ("%x ", Double.hashCode (-0));
047                System.out.format ("%x ", Double.hashCode (-1));
048                System.out.format ("%x ", Double.hashCode (-2));
049                System.out.format ("%x ", Double.hashCode (Double.MIN_VALUE));
050                System.out.println ();
051
052                System.out.format ("\nchar\n");
053                System.out.format ("%x ", Character.hashCode ('0'));
054                System.out.format ("%x ", Character.hashCode ('1'));
055                System.out.format ("%x ", Character.hashCode ('2'));
056                System.out.println ();
057
058                System.out.format ("\nstring\n");
059                System.out.format ("%x ", Objects.hashCode ("0"));
060                System.out.format ("%x ", Objects.hashCode ("1"));
061                System.out.format ("%x ", Objects.hashCode ("2"));
062                System.out.println ();
063
064                System.out.format ("\nstring decimal\n");
065                System.out.format ("%d ", Objects.hashCode ("0"));
066                System.out.format ("%d ", Objects.hashCode ("1"));
067                System.out.format ("%d ", Objects.hashCode ("2"));
068                System.out.println ();
069
070                System.out.format ("%d (=%d*31 + %d) ", Objects.hashCode ("00"), (int)'0', (int)'0');
071                System.out.format ("%d (=%d*31 + %d) ", Objects.hashCode ("01"), (int)'0', (int)'1');
072                System.out.format ("%d (=%d*31 + %d) ", Objects.hashCode ("02"), (int)'0', (int)'2');
073                System.out.println ();
074
075                System.out.format ("\nstring\n");
076                System.out.format ("%d ", Objects.hashCode ("0"));
077                System.out.format ("%d ", Objects.hashCode ("00"));
078                System.out.format ("%d ", Objects.hashCode ("000"));
079                System.out.format ("%d ", Objects.hashCode ("0000"));
080                System.out.format ("%d ", Objects.hashCode ("00000"));
081                System.out.format ("%d ", Objects.hashCode ("000000"));
082                System.out.format ("%d ", Objects.hashCode ("0000000"));
083                System.out.format ("%d ", Objects.hashCode ("00000000"));
084                System.out.format ("%d ", Objects.hashCode ("000000000"));
085                System.out.println ();
086
087                {
088                        System.out.format ("\nfloat 0's\n");
089                        float x = Float.intBitsToFloat (0);
090                        float y = Float.intBitsToFloat (0x80000000);
091                        System.out.format ("(x==y)=%b Float.compare(x,y)=%d\n", x==y, Float.compare (x, y));
092                        System.out.format ("x=%s Float.hashCode(x)=%x\n", x, Float.hashCode (x));
093                        System.out.format ("y=%s Float.hashCode(y)=%x\n", y, Float.hashCode (y));
094                }
095                {
096                        System.out.format ("\nfloat NaN's\n");
097                        float x = Float.NaN;
098                        float y = Float.NaN;
099                        System.out.format ("(x==y)=%b Float.compare(x,y)=%d\n", x==y, Float.compare (x, y));
100                        System.out.format ("x=%s Float.hashCode(x)=%x\n", x, Float.hashCode (x));
101                        System.out.format ("y=%s Float.hashCode(y)=%x\n", y, Float.hashCode (y));
102                }
103                {
104                        System.out.format ("\ndouble 0's\n");
105                        double x = Double.longBitsToDouble (0L);
106                        double y = Double.longBitsToDouble (0x8000000000000000L);
107                        System.out.format ("(x==y)=%b Double.compare(x,y)=%d\n", x==y, Double.compare (x, y));
108                        System.out.format ("x=%s Double.hashCode(x)=%x\n", x, Double.hashCode (x));
109                        System.out.format ("y=%s Double.hashCode(y)=%x\n", y, Double.hashCode (y));
110                }
111                {
112                        System.out.format ("\ndouble NaN's\n");
113                        double x = Double.NaN;
114                        double y = Double.NaN;
115                        System.out.format ("(x==y)=%b Double.compare(x,y)=%d\n", x==y, Double.compare (x, y));
116                        System.out.format ("x=%s Double.hashCode(x)=%x\n", x, Double.hashCode (x));
117                        System.out.format ("y=%s Double.hashCode(y)=%x\n", y, Double.hashCode (y));
118                }
119
120                /*
121                 * Objects and Arrays act like this:
122                 *
123                 * Objects.equals(a, b) { return (a == b) || (a != null && a.equals(b)); }
124                 *   if (a==b) return true;
125                 *   if (a==null || b==null || (! a.equals(b)) return false;
126                 *   return true;
127                 * }
128                 * Objects.hashCode(a)  {
129                 *   if (a==null) return 0;
130                 *   return a.hashCode();
131                 * }
132                 *
133                 * Arrays.equals(a, b) {
134                 *   if (a==b) return true;
135                 *   if (a==null || b==null || (a.length != b.length)) return false;
136                 *   for (int i=0; i<a.length; i++) if (! Objects.equals(a[i],b[i])) return false;
137                 *   return true;
138                 * }
139                 * Arrays.hashCode(a) {
140                 *   if (a == null) return 0;
141                 *   int result = 1;
142                 *   for (Object element : a) result = 31 * result + Objects.hashCode(element);
143                 *   return result;
144                 * }
145                 *
146                 * Arrays.deepEquals is similar, but recursively calls Arrays.equals(a[i],b[i]) for nested arrays
147                 * Arrays.deepHashCode is similar, but recursively calls Arrays.hashCode(element) for nested arrays
148                 */
149                {
150                        System.out.format ("\nObject\n");
151                        Object x = new Object ();
152                        Object y = new Object ();
153                        System.out.format ("(x==y)=%b Objects.equals(x,y)=%b\n", x==y, Objects.equals (x,y));
154                        System.out.format ("x=%s Objects.hashCode(x)=%x\n", x, Objects.hashCode (x));
155                        System.out.format ("y=%s Objects.hashCode(y)=%x\n", y, Objects.hashCode (y));
156                }
157                {
158                        System.out.format ("\nObject null\n");
159                        Object x = null;
160                        Object y = null;
161                        System.out.format ("(x==y)=%b Objects.equals(x,y)=%b\n", x==y, Objects.equals (x,y));
162                        System.out.format ("x=%s Objects.hashCode(x)=%x\n", x, Objects.hashCode (x));
163                        System.out.format ("y=%s Objects.hashCode(y)=%x\n", y, Objects.hashCode (y));
164                }
165                {
166                        System.out.format ("\nint[]\n");
167                        int a[] = new int[4];
168                        int b[] = new int[4];
169                        System.out.format ("(a==b)=%b Objects.equals(a,b)=%b Arrays.equals(a,b)=%b\n", a==b, Objects.equals (a,b), Arrays.equals(a,b));
170                        System.out.format ("a=%s Objects.hashCode(a)=%x Arrays.hashCode(a)=%x\n", a, Objects.hashCode (a), Arrays.hashCode (a));
171                        System.out.format ("b=%s Objects.hashCode(b)=%x Arrays.hashCode(b)=%x\n", b, Objects.hashCode (b), Arrays.hashCode (b));
172                }
173                {
174                        System.out.format ("\nint[] null\n");
175                        int a[] = null;
176                        int b[] = null;
177                        System.out.format ("(a==b)=%b Objects.equals(a,b)=%b Arrays.equals(a,b)=%b\n", a==b, Objects.equals (a,b), Arrays.equals(a,b));
178                        System.out.format ("a=%s Objects.hashCode(a)=%x Arrays.hashCode(a)=%x\n", a, Objects.hashCode (a), Arrays.hashCode (a));
179                        System.out.format ("b=%s Objects.hashCode(b)=%x Arrays.hashCode(b)=%x\n", b, Objects.hashCode (b), Arrays.hashCode (b));
180                }
181                {
182                        System.out.format ("\nObject[] (for most object arrays, Arrays.equals is enough)\n");
183                        String a[] = new String[] { new String("hi") };
184                        String b[] = new String[] { new String("hi") };
185                        System.out.format ("(a==b)=%b Objects.equals(a,b)=%b Arrays.equals(a,b)=%b Arrays.deepEquals(a,b)=%b\n", a==b, Objects.equals (a,b), Arrays.equals(a,b), Arrays.deepEquals(a,b));
186                        System.out.format ("a=%s Objects.hashCode(a)=%x Arrays.hashCode(a)=%x Arrays.deepHashCode(a)=%x\n", a, Objects.hashCode (a), Arrays.hashCode (a), Arrays.deepHashCode (a));
187                        System.out.format ("b=%s Objects.hashCode(b)=%x Arrays.hashCode(b)=%x Arrays.deepHashCode(b)=%x\n", b, Objects.hashCode (b), Arrays.hashCode (b), Arrays.deepHashCode (b));
188                }
189                {
190                        System.out.format ("\nint[][] (multidimensional arrays need Arrays.deepEquals)\n");
191                        int a[][] = new int[4][4];
192                        int b[][] = new int[4][4];
193                        System.out.format ("(a==b)=%b Objects.equals(a,b)=%b Arrays.equals(a,b)=%b Arrays.deepEquals(a,b)=%b\n", a==b, Objects.equals (a,b), Arrays.equals(a,b), Arrays.deepEquals(a,b));
194                        System.out.format ("a=%s Objects.hashCode(a)=%x Arrays.hashCode(a)=%x Arrays.deepHashCode(a)=%x\n", a, Objects.hashCode (a), Arrays.hashCode (a), Arrays.deepHashCode (a));
195                        System.out.format ("b=%s Objects.hashCode(b)=%x Arrays.hashCode(b)=%x Arrays.deepHashCode(b)=%x\n", b, Objects.hashCode (b), Arrays.hashCode (b), Arrays.deepHashCode (b));
196                }
197        }
198}