Java examples for java.lang:Object
Returns the hash-code of the specified object.
/*/*from w w w . j a v a2 s .co m*/ * Copyright (c) 2015-2016 QuartzDesk.com. * Licensed under the MIT license (https://opensource.org/licenses/MIT). */ //package com.java2s; import java.util.Arrays; public class Main { public static void main(String[] argv) throws Exception { Object o = "java2s.com"; System.out.println(safeHashCode(o)); } /** * Returns the hash-code of the specified object. If the specified object * is null, then this method returns 1, rather then throwing NullPointerException. * * @param o the object. * @return the object's hash-code, or 1 if the object is null. */ public static int safeHashCode(Object o) { int hash = 1; if (o != null) { if (o.getClass().isArray()) return Arrays.deepHashCode((Object[]) o); else hash = o.hashCode(); } return hash; } }