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

本文将以从int类型转为String类型的方法为例,讲解从其他类型转换到String类型的各种方法,以及其中的区别。

1.(String)强制转换一式

Object obj = new Integer(100);
String str = (String)obj;
System.out.println(str);

【失败】
此种情况可以在语法检测上通过,但是运行后会报错:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

2.(String)强制转换二式

String str = (String)100;


【失败】
从图中提示可以发现,IDE直接报错,提示不能从int类型转换为java.lang.String类型。

3.String特殊转换

String str = "" + 100;
System.out.println(str);

【成功】
参考的是Java规范。这里引用的是一个匿名的知乎用户的回答。原文链接:https://www.zhihu.com/question/57105649/answer/151613096

If only one operand expression is of type String, then string conversion (§5.1.11) is performed on the other operand to produce a string at run time.The result of string concatenation is a reference to a String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created string.
渣翻如下
如果其中一个操作数表达式是String,那么对另一个操作数执行字符串转换,以在运行时产生一个字符串。
字符串连接的结果是对一个String对象的引用,该对象是两个操作数字符串的连接。左侧操作数的字符位于新创建的字符串中右侧操作数的字符之前。

4.Integer.toString(int i)方法

String str = Integer.toString(100);
System.out.println(str);

【成功】
使用的是int的包装类中实现的toString方法。

5.String.valueOf(int i)方法

String str = String.valueOf(100);
System.out.println(str);

【成功】
我在查看源代码时发现调用的是Integer中的toString方法,也就是4的方法。

public static String valueOf(int i) {
    return Integer.toString(i);
}

但是查看网上的资料中写的都是调用这个方法

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

想了一下,发现自己有点傻,原来String.valueOf()方法经过很多重载,可以通过传入的参数类型加以区分,从而调用相应的toString()方法进行类型转换。
使用的全部是基本类型对应的包装类,类似的方法还有很多,例如:

public static String valueOf(long l) {
    return Long.toString(l);
}
public static String valueOf(float f) {
    return Float.toString(f);
}

6.总结

上述几种成功的方法中,只有String.valueOf()方法会首先对转换的对象进行判空检测,如果为空,则返回“null”字符串,以至于不会报出空指针异常。
所以,在遇到字符串转换的时候,首选String.valueOf()方法。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,006评论 19 139
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,752评论 0 38
  • java中String的常用方法 1、length()字符串的长度 例:char chars[]={'a','b'...
    赤赤有名阅读 6,343评论 0 10
  • 才离开没多久就开始 担心今天的你过得好不好 整个画面是你 想你想的睡不着 嘴嘟嘟那可爱的模样 还有在你身上香香的味...
    L君幽默阅读 3,081评论 0 0
  • 进入我镜头中的这对母子很偶然。我本来是想要拍她们背后紧闭的大门的照片,没想到母子俩闯入了我的镜头,那么自然,那么美...
    茶叶蛋的Cha阅读 2,875评论 0 3

友情链接更多精彩内容