Java Object to String toString(Object obj)

Here you can find the source of toString(Object obj)

Description

Converts the object to a string.

License

Open Source License

Parameter

Parameter Description
obj the object.

Return

the string.

Declaration

static String toString(Object obj) 

Method Source Code

//package com.java2s;
import java.util.List;

public class Main {
    /**//from   w w w .  j  a v a  2  s  . c o  m
     * Converts the object to a string.
     *
     * @param obj
     *            the object.
     *
     * @return the string.
     */
    static String toString(Object obj) {
        //        if (obj instanceof IdentifiableEntity) {
        //            // IdentifiableEntity
        //            return ((IdentifiableEntity) obj).toJSONString();
        //
        //        } else if (obj instanceof TcDirectProject) {
        //            // TcDirectProject
        //            return ((TcDirectProject) obj).toJSONString();
        //        } else if (obj instanceof DirectProjectFilter) {
        //            // DirectProjectFilter
        //            return ((DirectProjectFilter) obj).toJSONString();
        //        } else if (obj instanceof DirectProjectMetadataDTO) {
        //            // DirectProjectMetadataDTO
        //            return ((DirectProjectMetadataDTO) obj).toJSONString();
        //        } else if (obj instanceof List<?>) {
        //            // DirectProjectMetadataDTO
        //            return toString((List<?>) obj);
        //        }

        return "";//String.valueOf(obj);
    }

    /**
     * Converts the List to a string.
     *
     * @param obj
     *            the List (not <code>null</code>).
     *
     * @return the string.
     */
    private static String toString(List<?> obj) {
        StringBuilder sb = new StringBuilder("[");

        boolean first = true;
        for (Object element : obj) {
            if (!first) {
                // Append a comma
                sb.append(", ");
            }
            first = false;

            sb.append(toString(element));
        }

        sb.append("]");

        return sb.toString();
    }
}

Related

  1. objToString(Object obj)
  2. objToString(Object obj)
  3. objToString(Object obj)
  4. objToString(Object[] obj)
  5. toString(Object o)
  6. toString(Object obj)
  7. toString(Object object)
  8. toString(Object object)
  9. toString(Object object)