Object#tostring ():
Object object = getObject (); System.out.println (Object.ToString ());
In this method of use, because the public method is already in the Java.lang.Object class. ToString (), this method can be called for any Java object that is strictly meaningful. Note, however, that object must be guaranteed to be not a null value, otherwise the NullPointerException exception will be thrown. With this approach, the derived class typically overrides the ToString () method in object.
(String) Object
This is the standard type conversion, which converts an object to a value of type string. When using this method, it is important to note that the type must be able to be turned into a string type. Therefore, it is best to use instanceof as a type check to determine whether it can be converted. Otherwise it is easy to throw calsscastexception exceptions. In addition, it is particularly necessary to be especially careful that the syntax check does not cause an error when the object that is defined as type objects is turned into a string, which can lead to potential errors. Be extra careful at this point. Such as:
New Integer (= (String) obj;
Errors will occur at run time because the integer type is cast to a string type and cannot be passed. But
New Integer (= (String) obj;
In the case of a format code, a syntax error will be reported.
In addition, because null values can be cast to any Java class type, (String) NULL is also legal.
String.valueof (Object)
String.valueof (Object) is based on object#tostring (). But it's different from object#tostring (). As mentioned in the previous analysis, the use of the latter is guaranteed to be non-null. However, with this approach, you will not have to worry about whether object is a null value. For the sake of explaining the problem, let's analyze the relevant source code. JDK string#valueof (Object) source code is as follows:
Public Static String valueOf (Object obj) { returnnull)? "Null" : Obj.tostring ();}
From the above source code can be very clear to see the null value without worrying reasons. However, this also gives us a hidden danger. We should note that when object is null, the value of String.valueof (object) is the string "null", not null!!! Remember to keep in mind when using the process. Imagine if we were to use if (string.valueof (object) ==null) {System.out.println ("the value passed in is null! ”);} Such a statement will likely be problematic. Think again, when outputting to the console, visually the following statements differ in the result of the execution:
System.out.println (string.valueof (null)); SYSTEM.OUT.PRINTLN (null);
The output we see will be the exact same thing: null, but the meaning is different.
Object + ""
String addition, the final output is a string type, in fact, this is the result of operator overloading, any object and string to add the final output is a string type.
But! Note that as long as the string using the add symbol will automatically new a StringBuilder to append, the efficiency will increase the capacity consumption of more than one object, so it is recommended not to use this way to handle the string.
But the above is not necessarily correct, and now the JDK will be compiled to optimize, such as direct: System.out.println ("Hello World" + 1 "easonjim"), then the compiler will be converted to SYSTEM.OUT.PRINTLN (" Hello World1easonjim ").
But! If you define a variable in an additional place and assemble it with other object types, you will also use StringBuilder to append.
These practices can be tested using the JAD Anti-compilation tool.
Reference:
Http://blog.sina.com.cn/s/blog_986844ba01010y7z.html (The above sections are transferred from this article)
Http://www.blogjava.net/nokiaguy/archive/2008/05/07/198990.html (string addition analysis)
Http://www.blogjava.net/ljc-java/archive/2011/02/28/345344.html
The difference between string string ToString (), string.valueof (), string strong, + "" in Java