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 @   任国强  阅读(18805)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
历史上的今天:
2011-03-15 初识 Google Analytics 入门一
2011-03-15 WCF RIA Services Code Gallary To Explorer
2011-03-15 WCF RIA Services Code Gallary
2011-03-15 远程桌面ActiveX Control的引用代码
2011-03-15 初识JQuery 实例一
点击右上角即可分享
微信分享提示