Java May Use UTF-8 as Its Default Charset
marxsoftware.blogspot.com
marxsoftware.blogspot.com
Char is a 16bit type, so some unicode 'characters' have high/low surrogate pairs, those pairs are encoded in 32bit int as `codePoint`. Example of a problem: String#length returns number of chars, not number of codepoints.
String/Character api is a one big record of utf16 madness: http://grepcode.com/file/repository.grepcode.com/java/root/j... http://grepcode.com/file/repository.grepcode.com/java/root/j...
Most of contemporary technologies use UTF8 for string encoding and exchange format. This back and forth UTF8<>UTF16 conversion in java doesn't come without a computational&memory price.
(Some characters are considered by Unicode to be the same character despite the Japanese and Chinese ways of writing that character being visually distinguishable; Unicode regards this as the same character being written in different fonts (just as in our Latin alphabet there are multiple visually distinct ways to write the lower-case "a" or the number 4, one can choose to cross "7"s or not, etc.) but customers sometimes don't find that acceptable; in particular many Japanese people are very unhappy to see their names written with the Chinese versions of those characters, which tend to be how international fonts render them)
So the problem is the font?
None of this would be solved by devolving back to popular pre-unicode standards like EUC-JP or Big5.
You could do something like switching the font based on the system locale, but at that point you're no better off than when you were using the system's character encoding, and following a less well-trodden path.
The number of characters you need to cover is hard enough to make it a daunting task, and if Han Unification didn't exist it could have been 3 times harder...
The TRON Multilingual System (which was championed by the Han Unification opponents in the 90s) basically does second solution. It did avoid character unification between Chinese, Japanese and Korean, but it still chose to specify the language of the text to support different overall processing based on the language (e.g. different ligatures may be activated for different European languages).
If you're talking about Japanese users being unhappy that their name characters change when its rendered _within Chinese Text_ - well, that's how it's always been in newspapers, books and official documents. Signs all over Japan (made by Japanese for Chinese tourists) show the Chinese rendition of Japanese place names such as 涩谷 (Shibuya) and nobody seems to bother. It goes the other way too, of course.
Indeed, but - more by accident than design - traditional codepages end up making language-specific fonts easy, since you'll naturally use different fonts for different encodings (and if you don't, it's easy to switch to doing so, because you were already carrying around the encoding for each piece of text so you know which texts are Chinese and which are Japanese). I mean, using UTF-8 you could carry around that metadata manually and achieve the same result, but that's a lot less fail-fast; you usually only realise you needed that information after you've got a substantial amount of text of unknown language stored in your system. Whereas if you fail to store which encoding text is in then you (hopefully) notice sooner.
> Signs all over Japan (made by Japanese for Chinese tourists) show the Chinese rendition of Japanese place names such as 涩谷 (Shibuya) and nobody seems to bother.
Sure, which I think is what mislead the Unicode consortium - for ordinary text no-one cares, and most people's names aren't affected. But the people whose names are affected can take it quite personally.
Compared to that, the number of times people from Japan browsing at a site configured to display Japanese language with Chinese Fonts? That must happen once in Super Blue Blood Moon, but not that often. And "many" is definitely exaggeration, considering the number of Japanese name which use characters subject to heavily modified by unification. In other words, "bone" is not a popular character in Japanese names, who would have figured.
Han Unification is not without its pains, of course, and I still can't tell for sure whether it was a good idea, but using EUC-JP or Shift-JIS is not going to get you around it, since you still need a Japanese font to display the characters correctly as they should look in Japanese. And I would gladly take UTF-8 over EUC-JP and Shift-JIS every day.
UTF-8 is clearly superior.
The internal encoding of strings attracts too much attention. A proper string type abstracts over these details. When doing arithmetic in Haskell, Python, JavaScript, etc. you don't worry about how the numbers are encoded; strings should be the same.
UTF-8 overlong sequences haven't been a problem for a while.
The choice of encoding is definitely relevant for all external file formats, protocols, APIs. We can't just let the choice of encoding be purely internal, though I agree that programmers should not need to care about what their runtimes use internally for strings, as long as it is Unicode (UTF-8, 16, 32, or some other UTF say).
But no UTF actually gives you random access by character index because a) no runtime is going to be ensuring normalization of all strings to precomposed sequences (not even NFC because NFC is closed to new compositions!), b) not every valid character will have pre-composed codepoints therefore (a) isn't even possible. Given this, UTF-16 and UTF-32 have no real advantages over UTF-8, and the false sense of simplicity they convey is dangerous anyways.
> "Many Windows programs (including Windows Notepad) add the bytes 0xEF, 0xBB, 0xBF at the start of any document saved as UTF-8. This is the UTF-8 encoding of the Unicode byte order mark (BOM), and is commonly referred to as a UTF-8 BOM, even though it is not relevant to byte order. A BOM can also appear if another encoding with a BOM is translated to UTF-8 without stripping it. Software that is not aware of multibyte encodings will display the BOM as three garbage characters at the start of the document, e.g. "" in software interpreting the document as ISO 8859-1 or Windows-1252 or "" if interpreted as code page 437, a default for certain older Windows console applications."
> "The Unicode Standard neither requires nor recommends the use of the BOM for UTF-8, but warns that it may be encountered at the start of a file as a transcoding artifact. The presence of the UTF-8 BOM may cause problems with existing software that can handle UTF-8..."
https://docs.microsoft.com/en-us/globalization/encoding/byte...