Java:为什么字符集名称不是常量? [英] Java: Why charset names are not constants?

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

问题描述

字符集问题本身是混乱和复杂的,但除此之外,你必须记住字符集的确切名称。是utf8吗?或utf-8?或者可能UTF-8?当搜索互联网的代码样本,你会看到所有上述。为什么不让他们命名常量,并使用 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?

推荐答案

但是,有六个需要存在,因此常量可以已经做了很久以前。我不知道为什么他们不是。

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实例。那么为什么不提供六个标准的字符集常量呢?我问马丁·布霍尔茨,因为他恰好坐在我旁边,他说没有一个真正特别的理由,除了当时,事情仍然半烤 - 太少的JDK API已经改装接受Charset和那些,Charset重载通常执行得稍差。

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中,他们终于完成了所有的Charset重载。而这种倒退的表现情况仍然存在(为什么是令人难以置信的奇怪的原因,我不能解释,但是与安全相关。)

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!).

长故事短 - - 只是定义你自己的常量,或使用Guilla的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.

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

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