Java Object to String toString(Object object)

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

Description

to String

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    
    public static String toString(Object object) 

Method Source Code


//package com.java2s;
/* NOTICE//from  w  w w  .  j a va2 s . c  o  m
 *
 * Copyright 2006 - 2008 Grant Gardner
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed
 * to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 *
 NOTICE */

import java.util.Arrays;

public class Main {
    @SuppressWarnings("unchecked")
    /**
     * Crude way to handle nice array printing.
     */
    public static String toString(Object object) {

        String result;
        if (object.getClass().isArray()) {
            Class readingClass = object.getClass().getComponentType();
            if (Integer.TYPE.equals(readingClass)) {
                result = Arrays.toString((int[]) object);
            } else if (Long.TYPE.equals(readingClass)) {
                result = Arrays.toString((long[]) object);
            } else if (Double.TYPE.equals(readingClass)) {
                result = Arrays.toString((double[]) object);
            } else if (Float.TYPE.equals(readingClass)) {
                result = Arrays.toString((float[]) object);
            } else if (Character.TYPE.equals(readingClass)) {
                result = Arrays.toString((char[]) object);
            } else if (Boolean.TYPE.equals(readingClass)) {
                result = Arrays.toString((boolean[]) object);
            } else if (Short.TYPE.equals(readingClass)) {
                result = Arrays.toString((short[]) object);
            } else {
                result = Arrays.toString((Object[]) object);
            }
        } else {
            result = object.toString();
        }
        return result;
    }
}

Related

  1. toString(Object obj)
  2. toString(Object obj)
  3. toString(Object object)
  4. toString(Object object)
  5. toString(Object object)
  6. toString(Object object, Class objectClass)
  7. toString(Object val)
  8. toString(Object value)
  9. toString(Object value)