IAmYourFaja IAmYourFaja - 7 months ago 21
Java Question

Why no Java Charset setEncoding method?

I found this excellent SO question asking for the distinction between a character set vs. character encoding. And it makes sense: essentially the character set is the set of glyphs available for use, and its respective encoding is how each glyph translates to and from binary.

I then went to the Java 7 SE

API doc and was surprised only to see a
getEncoding()
method but no respective setter. So it seems that, at least in Java land, every character set gets "bound" to a pre-configured encoding.

This got me thinking: why is there no setter here? Why does Java not allow the user to define what encoding to use for binding a set of characters to binary?

Along those same lines, what if Java doesn't support a particular character set/encoding? Is there a way to extend the JRE with custom sets/encodings?

Answer

Because what Java calls a Charset is what you call an encoding. The documentation of CharSet describes a Charset as:

A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes.

BTW, there is no getEncoding() method in Charset.

Comments