为什么字符集名称不是常量?

Why charset names are not constants?(为什么字符集名称不是常量?)
本文介绍了为什么字符集名称不是常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符集问题本身就令人困惑和复杂,但最重要的是您必须记住字符集的确切名称.是 "utf8" 吗?还是"utf-8"?或者UTF-8"?在 Internet 上搜索代码示例时,您将看到以上所有内容.为什么不让它们命名为常量并使用 Charset.UTF8?

Charset issues are confusing and complicated by themselves, but on top of that you have to remember exact names of your charsets. Is it "utf8"? Or "utf-8"? Or maybe "UTF-8"? When searching internet for code samples you will see all of the above. Why not just make them named constants and use Charset.UTF8?

推荐答案

问题的简单答案是可用的字符集字符串因平台而异.

The simple answer to the question asked is that the available charset strings vary from platform to platform.

然而,有六个是必须存在的,所以很早以前就可以为那些常量创建常量了.我不知道他们为什么没有.

However, there are six that are required to be present, so constants could have been made for those long ago. I don't know why they weren't.

JDK 1.4 通过引入 Charset 类型做了一件了不起的事情.在这一点上,他们不想再提供 String 常量,因为目标是让每个人都使用 Charset 实例.那么为什么不提供六个标准的 Charset 常量呢?我问了 Martin Buchholz,因为他正好坐在我旁边,他说没有什么特别好的理由,只是当时事情还没有完全成熟——改进的 JDK API 太少了接受字符集,在那些字符集重载中,字符集重载的性能通常稍差.

JDK 1.4 did a great thing by introducing the Charset type. At this point, they wouldn't have wanted to provide String constants anymore, since the goal is to get everyone using Charset instances. So why not provide the six standard Charset constants, then? I asked Martin Buchholz since he happens to be sitting right next to me, and he said there wasn't a really particularly great reason, except that at the time, things were still half-baked -- too few JDK APIs had been retrofitted to accept Charset, and of the ones that were, the Charset overloads usually performed slightly worse.

令人遗憾的是,只有在 JDK 1.6 中,他们才最终完成了对所有字符集重载的装配.而且这种性能倒退的情况仍然存在(原因非常奇怪,我无法解释,但与安全有关!).

It's sad that it's only in JDK 1.6 that they finally finished outfitting everything with Charset overloads. And that this backwards performance situation still exists (the reason why is incredibly weird and I can't explain it, but is related to security!).

长话短说——只需定义您自己的常量,或使用与 Tony the Pony 链接的 Guava 的 Charsets 类(尽管该库实际上尚未真正发布).

Long story short -- just define your own constants, or use Guava's Charsets class which Tony the Pony linked to (though that library is not really actually released yet).

更新:StandardCharsets 类在 JDK 7 中.

Update: a StandardCharsets class is in JDK 7.

这篇关于为什么字符集名称不是常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)