zoukankan      html  css  js  c++  java
  • 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());
      }

     这些自己更放心啦。








  • 相关阅读:
    批量下载B站视频
    MATLAB安装
    printf小结
    hdoj 1874 dijkstra
    斐波那契而数列
    PAT数列排序
    感想
    物理定律的适用范围
    本博客的文章曾经恢复过,但迅速被部分删除(不知名的原因)
    (废墟重建)程序员如何学习书本上的例子 how programmers learn examples of books
  • 原文地址:https://www.cnblogs.com/rgqancy/p/5280036.html
Copyright © 2011-2022 走看看