toString of of String valueOf of methods

  • 2020-06-01 09:47:43
  • OfStack

In the actual development and application of java projects, the basic function of converting objects to String is often used. This article will summarize the common transformation methods. Common methods include Object.toString (), (String) objects to convert, String.valueOf (Object), and so on. These methods 11 are analyzed below.

Method 1: use the Object.toString() method. See the following example:


Object object = getObject();
System.out.println(object.toString());

In this usage method, because the Java.lang.Object class already has an public method.toString (), this method can be called on any strictly java object. Note, however, that you must ensure that object is not an null value, or you will throw an NullPointerException exception. With this approach, it is common for derived classes to override the toString () method in Object.

Method 2: use the type conversion (String) object method, which is a standard type conversion, to convert object to a value of type String. When using this method, it is important to note that the type must be able to convert to the String type. So it's best to do a type check with instanceof to see if you can convert. Otherwise it is easy to throw an CalssCastException exception. In addition, special care should be taken as objects of type Object are not syntax checked for errors when they are converted to String, which can lead to potential errors. Be extra careful. Such as:


Object obj = new Integer(100);
String strVal = (String)obj;

There will be an error at run time because casting Integer to String will not pass. However,


Integer obj = new Integer(100);
String strVal = (String)obj;

If this is the format code, a syntax error will be reported.

In addition, since the null value can be cast to any java class type, (String)null is also legal.

方法3: using String.valueOf(Object) String.valueOf(Object)的基础是Object.toString()。 But it's different from Object#toString(). As mentioned in the previous analysis of method 1, you need to ensure that the latter is not null. However, with the third method, you do not have to worry about the 1 question whether object is the null value. To illustrate the problem, let's analyze the 1 related source code. Jdk String.valueOf (Object) source code is as follows:


public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString(); }

It is clear from the above source code why the null value should not worry. However, this also gives us precisely the hidden danger. We should note that when object is null, the value of String.valueOf (object) is the string "null", not null!! Remember to pay attention when using it. So imagine 1, if we use theta

if(String.valueOf(object)==null){System.out.println(“传入的值是null!”);} What could possibly go wrong with such a statement? Think again about the visual difference between the results of execution of the following statements when output to the console:


System.out.println(String.valueOf(null));// Is a string." null " 
System.out.println(null);// Is a null value null

The output we see will be a 1-mode, 1-like thing: null, but do they make the same sense?


Related articles: