Java Object to String objectToString(Object x)

Here you can find the source of objectToString(Object x)

Description

object To String

License

Creative Commons License

Declaration

private static String objectToString(Object x) 

Method Source Code

//package com.java2s;

public class Main {
    private static String objectToString(Object x) {
        // Extreme compatibility with StringBuilder.append(null)
        String s;//  ww  w  .j  a  va 2s  . c o  m
        return (x == null || (s = x.toString()) == null) ? "null" : s;
    }

    /**
     * Like Arrays.toString(), but caller guarantees that size > 0,
     * each element with index 0 <= i < size is a non-null String,
     * and charLength is the sum of the lengths of the input Strings.
     */
    static String toString(Object[] a, int size, int charLength) {
        // assert a != null;
        // assert size > 0;

        // Copy each string into a perfectly sized char[]
        // Length of [ , , , ] == 2 * size
        final char[] chars = new char[charLength + 2 * size];
        chars[0] = '[';
        int j = 1;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                chars[j++] = ',';
                chars[j++] = ' ';
            }
            String s = (String) a[i];
            int len = s.length();
            s.getChars(0, len, chars, j);
            j += len;
        }
        chars[j] = ']';
        // assert j == chars.length - 1;
        return new String(chars);
    }
}

Related

  1. objectToString(Object obj, String defaultvalue)
  2. objectToString(Object object)
  3. objectToString(Object object, String defaultValue)
  4. objectToString(Object objectStr)
  5. ObjectToString(Object x)
  6. objectToString(Object[] obj)
  7. objectToStringNoNull(Object obj)
  8. objToString(Object obj)
  9. objToString(Object obj)