Java String Value Of valueOf(final Object obj, final String defaultValue)

Here you can find the source of valueOf(final Object obj, final String defaultValue)

Description

 Assertions : assertNull(StringHelper.valueOf(null, null)); assertEquals("", StringHelper.valueOf(null, "")); assertEquals("a", StringHelper.valueOf(null, "a")); assertEquals("foo", StringHelper.valueOf("foo", "a")); 

License

Apache License

Parameter

Parameter Description
obj a parameter
defaultValue the default value to return if object is null

Return

the String representation of the object instance.
It uses , but if arg is null, it will return defaultValue

Declaration

public static String valueOf(final Object obj, final String defaultValue) 

Method Source Code

//package com.java2s;
/*/* ww  w.jav  a  2  s. com*/
 * Copyright 2008-2010 the original author or authors
 *
 * 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.
 */

public class Main {
    /**
     * <p>
     * 
     * <pre>
     * <b>Assertions :</b>
     *    assertNotNull(StringHelper.valueOf(""));
     *    assertEquals("", StringHelper.valueOf(""));
     *    assertEquals("foo", StringHelper.valueOf("foo"));
     * </pre>
     * 
     * </p>
     * 
     * @param obj
     * @return the String representation of the object instance. <br/>
     *         It uses {@link String#valueOf(Object)}, but if arg is null, it will return empty String ""
     */
    public static String valueOf(final Object obj) {
        return valueOf(obj, "");
    }

    /**
     * <p>
     * 
     * <pre>
     * <b>Assertions :</b>
     *    assertNull(StringHelper.valueOf(null, null));
     *    assertEquals("", StringHelper.valueOf(null, ""));
     *    assertEquals("a", StringHelper.valueOf(null, "a"));
     *    assertEquals("foo", StringHelper.valueOf("foo", "a"));
     * </pre>
     * 
     * </p>
     * 
     * @param obj
     * @param defaultValue the default value to return if object is null
     * @return the String representation of the object instance. <br/>
     *         It uses {@link String#valueOf(Object)}, but if arg is null, it will return defaultValue
     */
    public static String valueOf(final Object obj, final String defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return String.valueOf(obj);
        }
    }
}

Related

  1. valueOf(Class enumType, String name, T defaultValue)
  2. valueOf(final Class enumType, final String value)
  3. valueOf(final Class type, final String value)
  4. valueOf(final Class clazz, final String name)
  5. valueOf(final Class enumType, final String name)
  6. valueOf(Object object, String defaultEmptyValue)
  7. valueOf(String boolStr)
  8. valueOf(String decimal)
  9. valueOf(String first, String defaultstr)