String.valueOf(object)和object.toString()的区别

假设有个对象object,如果object不是null,那么两者是没啥区别的。但是如果object是null,对于String.valueOf(object)返回的是null(从源码可以看出,已经对object做了判断),而object.toString()则会报空指针异常。
两种方法的源码code如下:
String.valueOf(object)的源码:

    /**
     * Returns the string representation of the <code>Object</code> argument.
     *
     * @param   obj   an <code>Object</code>.
     * @return  if the argument is <code>null</code>, then a string equal to
     *          <code>"null"</code>; otherwise, the value of
     *          <code>obj.toString()</code> is returned.
     * @see     java.lang.Object#toString()
     */
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }

object.toString()的源码:

    /**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return  a string representation of the object.
     */
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
`String.valueOf()`和`toString()`都是用于将对象转换为字符串的方法,但它们之间有一些区别。 1. `String.valueOf()`方法:`String.valueOf()`是一个静态方法,它接受一个参数并返回与参数对应的字符串表示。这个方法可以处理各种类型的参数,包括基本数据类型和对象。如果参数是一个对象,它会调用对象的`toString()`方法来获取字符串表示。如果参数是`null`,它会返回字符串"null"。 以下是使用`String.valueOf()`的示例: ```java int num = 10; String str1 = String.valueOf(num); // "10" Double d = 3.14; String str2 = String.valueOf(d); // "3.14" Object obj = new Object(); String str3 = String.valueOf(obj); // 调用obj的toString()方法来获取字符串表示 String str4 = String.valueOf(null); // "null" ``` 2. `toString()`方法:`toString()`是一个定义在`Object`类中的方法,所有的Java对象都继承了这个方法。默认情况下,`toString()`返回一个包含对象类名和哈希码的字符串表示。但是,你可以根据需要在自定义类中重写这个方法,以便返回更有意义的字符串表示。 以下是重写`toString()`方法的示例: ```java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } public class Main { public static void main(String[] args) { Person person = new Person("John", 25); String str = person.toString(); // "Person{name='John', age=25}" } } ``` 在上面的示例中,`Person`类重写了`toString()`方法,以返回一个包含姓名和年龄的字符串表示。 总结来说,`String.valueOf()`是一个静态方法,用于将任何类型的参数转换为字符串表示,而`toString()`是一个在`Object`类中定义的方法,用于将对象转换为字符串。如果你需要获取一个对象的字符串表示,通常最好使用`toString()`方法,在自定义类中重写这个方法以提供更有意义的字符串表示。如果你只是想将其他类型转换为字符串,可以使用`String.valueOf()`方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值