简体   繁体   English

转换为String和String.valueOf之间的区别

[英]Difference between casting to String and String.valueOf

What is the difference between 之间有什么区别

Object foo = "something";
String bar = String.valueOf(foo);

and

Object foo = "something";
String bar = (String) foo;

Casting to string only works when the object actually is a string: 仅当对象实际上是字符串时,才强制转换为字符串:

Object reallyAString = "foo";
String str = (String) reallyAString; // works.

It won't work when the object is something else: 当对象是其他对象时,它将不起作用:

Object notAString = new Integer(42);
String str = (String) notAString; // will throw a ClassCastException

String.valueOf() however will try to convert whatever you pass into it to a String . 但是String.valueOf()会尝试将传递给它的任何内容转换为String It handles both primitives ( 42 ) and objects ( new Integer(42) , using that object's toString() ): 它处理原语( 42 )和对象(使用该对象的toString() new Integer(42) toString() ):

String str;
str = String.valueOf(new Integer(42)); // str will hold "42"
str = String.valueOf("foo"); // str will hold "foo"
Object nullValue = null;
str = String.valueOf(nullValue); // str will hold "null"

Note especially the last example: passing null to String.valueOf() will return the string "null" . 特别注意最后一个示例:将null传递给String.valueOf()将返回字符串"null"

String.valueOf(foo) invokes foo 's .toString() method and assigns the result to the the bar . String.valueOf(foo)调用foo.toString()方法,并将结果分配给bar It is null and type safe operation. 它为null,并输入安全操作。

Casting will just assign foo to the bar , if the types are matching. 如果类型匹配,则仅将foo赋给bar Otherwise, the expression will throw a ClassCastException . 否则,表达式将抛出ClassCastException

Both generates same output in case of String . String情况下,两者都生成相同的输出

Casting fails in case of provided object is Not a string. 如果提供的对象不是string.Casting失败string.

强制转换意味着该对象必须为String类型,而String.valueOf()也可以采用其他类型。

String.valueOf method is used to get the String represenation of it's parameter object. String.valueOf方法用于获取其参数对象的String表示形式。

(String) value casts object value to string. (字符串)值将对象值转换为字符串。

You can use the String.valueOf method to get the String representation of an object without worrying about null references. 您可以使用String.valueOf方法获取对象的String表示形式,而不必担心null引用。 If you try to cast String on a null reference you would get a NullPointerException. 如果尝试将String强制转换为null引用,则将获得NullPointerException。

 final Object obj = null; final String strValOfObj = String.valueOf(obj); final String strCastOfObj = (String) obj; if (strValOfObj == null) System.out.println("strValOfObj is null"); if (strCastOfObj == null) System.out.println("strCastOfObj is null"); 

Output : strCastOfObj is null 输出strCastOfObj is null

The first one ie, String.valueOf returns a string only if the object is a representable type which is a value type or a String.. Else it throws the exception. 第一个(即String.valueOf仅在对象是可表示类型(它是值类型)或String时才返回字符串。否则它将引发异常。

In the latter one, you are directly casting which can fail if the object isn't a string. 在后一种情况下,您将直接强制转换,如果对象不是字符串,则可能会失败。

Online example. 在线示例。

http://ideone.com/p7AGh5 http://ideone.com/p7AGh5

in String.valueOf(); 在String.valueOf();中 string as work typecasting all the argument passed in valueof() method convert in String and just like integer.string() convert integer into string only 字符串作为工作类型广播在valueof()方法中传递的所有参数都在String中转换,就像integer.string()仅将整数转换为字符串一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM