Java String hashCode of null string
问题 Only interesting, why method hashCode() in java.lang.String is not static? And in case of null return e.g. -1 ? Because frequently need do somethihg like: String s; ............. if (s==null) { return 0;} else { return s.hashCode(); } Thanks. 回答1: As others have noted hashCode is a method on Object and is non-static because it inherently relies (i.e. belongs to) an object/instance. Note that Java 7 introduced the Objects class, which has the hashCode(Object) method, which does exactly what