java中CharSequence和String之间的确切区别 [英] Exact difference between CharSequence and String in java

查看:1023
本文介绍了java中CharSequence和String之间的确切区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此上一篇文章。任何人都可以说 CharSequence 和String之间的确切区别,除了 String 实现 CharSequence String 是一个字符序列?例如:

I read this previous post. Can any one say what the exact difference between CharSequence and String is, other than the fact that String implements CharSequence and that String is a sequence of character? For example:

CharSequence obj = "hello";
String str = "hello";
System.out.println("output is : " + obj + "  " + str);

将hello分配给 obj 再次到 str

What happens when "hello" is assigned to obj and again to str ?

推荐答案

一般差异



有几个类实现 CharSequence 界面/api/index.html?java/lang/String.html\"rel =noreferrer> 字符串 。其中包括

General differences

There are several classes which implement the CharSequence interface besides String. Among these are

  • StringBuilder for variable-length character sequences which can be modified
  • CharBuffer for fixed-length low-level character sequences which can be modified

任何接受 CharSequence 的方法都可以在所有这些方法上运行。任何只接受 String 的方法都需要转换。所以在所有你不关心内部的地方使用 CharSequence 作为参数类型是谨慎的。但是,如果实际返回 String ,则应使用 String 作为返回类型,因为这样可以避免返回值的可能转换如果调用方法确实需要字符串

Any method which accepts a CharSequence can operate on all of these equally well. Any method which only accepts a String will require conversion. So using CharSequence as an argument type in all the places where you don't care about the internals is prudent. However you should use String as a return type if you actually return a String, because that avoids possible conversions of returned values if the calling method actually does require a String.

另请注意,地图应使用字符串作为键类型,而不是 CharSequence ,因为映射键不能更改。换句话说,有时 String 的不可变性质是必不可少的。

Also note that maps should use String as key type, not CharSequence, as map keys must not change. In other words, sometimes the immutable nature of String is essential.

至于您粘贴的代码:只需编译它,然后使用 javap -v 查看JVM字节码。在那里你会注意到 obj str 都是对同一个常量对象的引用。由于 String 是不可变的,所以这种共享是可以的。

As for the code you pasted: simply compile that, and have a look at the JVM bytecode using javap -v. There you will notice that both obj and str are references to the same constant object. As a String is immutable, this kind of sharing is all right.

+ 运算符 String 被编译为各种 StringBuilder.append 调用的调用。所以它相当于

The + operator of String is compiled as invocations of various StringBuilder.append calls. So it is equivalent to

System.out.println(
  (new StringBuilder())
  .append("output is : ")
  .append((Object)obj)
  .append(" ")
  .append(str)
  .toString()
)

我必须承认我对我的编译器 javac有点惊讶1.6.0_33 使用 StringBuilder.append(Object)编译 + obj 而不是 StringBuilder.append(CharSequence的)。前者可能涉及对对象的 toString()方法的调用,而后者应该可以更有效的方式。另一方面, String.toString()只返回 String 本身,因此没有什么惩罚。因此,通过大约一个方法调用, StringBuilder.append(String)可能更有效。

I must confess I'm a bit surprised that my compiler javac 1.6.0_33 compiles the + obj using StringBuilder.append(Object) instead of StringBuilder.append(CharSequence). The former probably involves a call to the toString() method of the object, whereas the latter should be possible in a more efficient way. On the other hand, String.toString() simply returns the String itself, so there is little penalty there. So StringBuilder.append(String) might be more efficient by about one method invocation.

这篇关于java中CharSequence和String之间的确切区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆