Java 编码规范 StandardCharsets.UTF_8 三个方法 toString() name() displayName(),到底用哪个方法更合适?

想用StandardCharsets.UTF_8 返回"UTF-8"这个字符,测试一下,三个方法toString() name() displayName(),均能返回"UTF-8",到底用哪个方法更健壮?

public final String name()

Returns this charset's canonical(正式的) name.
=========================================
public String displayName()

Returns this charset's human-readable(便于人类可阅读的) name for the default locale.

The default implementation of this method simply returns this charset's canonical name. Concrete subclasses of this class may override this method in order to provide a localized display name.
==========================================
public final String toString()

Returns a string describing this charset.

==========================================

一分析,自然是name displayName比toString更合适,二选一,找区别自然选择name,因为我是要给程序传递参数,而不是输出给人机界面。

另外,找了一个最好的例证,jdk有这样的用法:
 package java.util.zip;
final class ZipCoder

  private ZipCoder(Charset cs) {
    this.cs = cs;
    this.isUTF8 = cs.name().equals(StandardCharsets.UTF_8.name());
  }

 这些自己更放心啦。








posted @ 2016-03-15 16:32  任国强  阅读(18520)  评论(0编辑  收藏  举报