Summary of the differences between String, String.valueOf, toString

When I used this today, I found that the three of them seem to be available in some places, but it inevitably makes people think that since the three of them are so similar, there must be some difference. I also found some information online. I have read the API documentation myself, so I will summarize the differences between the three. It took so long to find out that there are such differences, which is also quite ashamed.

  Let's talk about the functions of the three of them first: there is no special function, that is, the object or parameter type we get is converted into the form of a string according to the requirements.

  1. String: There is no doubt that this is the form of forced conversion, which is simple, convenient and efficient. Java programmers may be excited to see high efficiency, but it has its downsides, which are limitations. Nothing in the Java world is without its flaws, there are always some good things and some bad things. The bad thing is that he can only force something that is originally a string. If it is not a string, then an error will be reported. for example:
    1
    2
    3
    4
    5
    Boolean boolean1 =  true ;
             
    String str3 = (String)boolean1; //这行代码会报错,编译不过
    String str4 = boolean1.toString(); //正常
    String str5 = String.valueOf(boolean1); //正常

    Through the above example, we can clearly understand that the way to force String is easy, so it has great limitations. Be cautious when using it.

  2. toString(): says so in the API documentation, returns the object itself (it's already a string!!!). According to its meaning, general objects or parameters have the method toString(), but it should be noted that there is no such method when a parameter is defined as int type. Also, when the parameter is empty, the .toString() method will report a null pointer exception, which is a bad part of this method and needs to be carefully considered when using it. for example:
    1
    2
    Object obj = getObject();
    Syystem.out.println(obj.toString());

     As the above code shows, if obj is not empty, it can be compiled normally, then if obj is taken out and empty, a null pointer exception will be reported.

  3. 最重要的角色出场了String.valueOf():这个方法是静态的,直接通过String调用,可以说是完美,只是平时不习惯这样写而已,这样的实现避免了前面两个的不足和缺点。首先来看看他内部的实现机制:
    1
    public  static  String valueOf(Object obj){ return  (obj== null ) ?  "null"  : obj.toString()};

     在内部就是做了为空的判断的,所以就不会报出空指针异常。

    总结:
      这三者的使用,个人觉得应该使用String.valueOf()的方式。这样的使用安全可靠,不会带来异常。说的有不对的地方,希望大家指正批评。

再重点介绍valueOf。

举个例子:

[html]  view plain  copy
  1. String teString=null;  
  2.             teString=String.valueOf(teString);  
  3.             System.out.println(teString);  
这样的写法,因为确定了传入的是String类型,在valueOf调用对应构造函数时不会发生错误,因此,返回的是字符串“null”。
[html]  view plain  copy
  1. String.valueOf(null);  
但如果改为以上的写法,则提示空指针。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325710079&siteId=291194637