• java toString方法


        //---------------------------------------------------------------------
        // Convenience methods for toString output
        //---------------------------------------------------------------------
    
        /**
         * Return a String representation of an object's overall identity.
         * @param obj the object (may be {@code null})
         * @return the object's identity as String representation,
         * or an empty String if the object was {@code null}
         */
        public static String identityToString(Object obj) {
            if (obj == null) {
                return EMPTY_STRING;
            }
            return obj.getClass().getName() + "@" + getIdentityHexString(obj);
        }
    
        /**
         * Return a hex String form of an object's identity hash code.
         * @param obj the object
         * @return the object's identity code in hex notation
         */
        public static String getIdentityHexString(Object obj) {
            return Integer.toHexString(System.identityHashCode(obj));
        }
    
        /**
         * Return a content-based String representation if {@code obj} is
         * not {@code null}; otherwise returns an empty String.
         * <p>Differs from {@link #nullSafeToString(Object)} in that it returns
         * an empty String rather than "null" for a {@code null} value.
         * @param obj the object to build a display String for
         * @return a display String representation of {@code obj}
         * @see #nullSafeToString(Object)
         */
        public static String getDisplayString(Object obj) {
            if (obj == null) {
                return EMPTY_STRING;
            }
            return nullSafeToString(obj);
        }
    
        /**
         * Determine the class name for the given object.
         * <p>Returns {@code "null"} if {@code obj} is {@code null}.
         * @param obj the object to introspect (may be {@code null})
         * @return the corresponding class name
         */
        public static String nullSafeClassName(Object obj) {
            return (obj != null ? obj.getClass().getName() : NULL_STRING);
        }
    
        /**
         * Return a String representation of the specified Object.
         * <p>Builds a String representation of the contents in case of an array.
         * Returns {@code "null"} if {@code obj} is {@code null}.
         * @param obj the object to build a String representation for
         * @return a String representation of {@code obj}
         */
        public static String nullSafeToString(Object obj) {
            if (obj == null) {
                return NULL_STRING;
            }
            if (obj instanceof String) {
                return (String) obj;
            }
            if (obj instanceof Object[]) {
                return nullSafeToString((Object[]) obj);
            }
            if (obj instanceof boolean[]) {
                return nullSafeToString((boolean[]) obj);
            }
            if (obj instanceof byte[]) {
                return nullSafeToString((byte[]) obj);
            }
            if (obj instanceof char[]) {
                return nullSafeToString((char[]) obj);
            }
            if (obj instanceof double[]) {
                return nullSafeToString((double[]) obj);
            }
            if (obj instanceof float[]) {
                return nullSafeToString((float[]) obj);
            }
            if (obj instanceof int[]) {
                return nullSafeToString((int[]) obj);
            }
            if (obj instanceof long[]) {
                return nullSafeToString((long[]) obj);
            }
            if (obj instanceof short[]) {
                return nullSafeToString((short[]) obj);
            }
            String str = obj.toString();
            return (str != null ? str : EMPTY_STRING);
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(Object[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(String.valueOf(array[i]));
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(boolean[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(byte[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(char[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append("'").append(array[i]).append("'");
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(double[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(float[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
    
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(int[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(long[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
    
        /**
         * Return a String representation of the contents of the specified array.
         * <p>The String representation consists of a list of the array's elements,
         * enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
         * by the characters {@code ", "} (a comma followed by a space). Returns
         * {@code "null"} if {@code array} is {@code null}.
         * @param array the array to build a String representation for
         * @return a String representation of {@code array}
         */
        public static String nullSafeToString(short[] array) {
            if (array == null) {
                return NULL_STRING;
            }
            int length = array.length;
            if (length == 0) {
                return EMPTY_ARRAY;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length; i++) {
                if (i == 0) {
                    sb.append(ARRAY_START);
                }
                else {
                    sb.append(ARRAY_ELEMENT_SEPARATOR);
                }
                sb.append(array[i]);
            }
            sb.append(ARRAY_END);
            return sb.toString();
        }
  • 相关阅读:
    [ACM]HDU Problem 2000 + Java
    [测试开发面试]zyb面试题总结
    [ACM]HDU Problem 1001 + Java
    [ACM]HDU Problem 1002 + Java
    [ACM]HDU Problem 1000 + Java
    [Intellij Idea]激活与配置
    [Android开发]合集(随时更新)
    [Android开发]前端样式设计合集(随时更新)
    [Android开发]emulator无法启动的问题
    [Intellij Idea] IDE使用技巧帖集合(随时更新)
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4027907.html
Copyright © 2020-2023  润新知