Returns the string representation of the specified object as returned by the object's toString() method, or null if the object is null. - Java java.lang

Java examples for java.lang:Object

Description

Returns the string representation of the specified object as returned by the object's toString() method, or null if the object is null.

Demo Code

/*//  www .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;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object obj = "java2s.com";
        System.out.println(safeToString(obj));
    }

    /**
     * Returns the string representation of the specified object
     * as returned by the object's <code>toString()</code> method,
     * or null if the object is null.
     *
     * @param obj an object.
     * @return the string representation of the specified object.
     */
    public static String safeToString(Object obj) {
        return obj != null ? obj.toString() : null;
    }
}

Related Tutorials