Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I would imagine the reason was fast, array like access to the character at index, but some characters won't fit into 16 bits, so it wouldn't work...

So if you have to handle special cases anyways, why not just use UTF-8?

share|improve this question
4  
Something to ask the Java designers, not the community at large. Voting to close as not constructive. –  Oded Nov 7 '12 at 13:42
11  
@Oded: absolutely unwarranted, as DeadMG's answer shows. –  Michael Borgwardt Nov 7 '12 at 14:11
    
I'm confused: I was pretty sure that this question was already answered (both here and on SO), but I can't find the duplicate(s). –  Joachim Sauer Nov 7 '12 at 18:42

2 Answers 2

up vote 28 down vote accepted

Because it used to be UCS-2, which was a nice fixed-length 16-bits. Of course, 16bit turned out not to be enough. They retrofitted UTF-16 in on top.

share|improve this answer
2  
whoa buzz words, buzz words! Care to explain further? UCS-2? –  rogerdpack Nov 7 '12 at 20:43
    
@rogerdpack: see en.wikipedia.org/wiki/UCS-2 –  DaveE Nov 7 '12 at 21:32
3  
Here is a quote from the Unicode FAQ: Originally, Unicode was designed as a pure 16-bit encoding, aimed at representing all modern scripts. (Ancient scripts were to be represented with private-use characters.) Over time, and especially after the addition of over 14,500 composite characters for compatibility with legacy sets, it became clear that 16-bits were not sufficient for the user community. Out of this arose UTF-16. At the time of Java release UTF-16 hasn't yet appeared, and UTF-8 was not a part of Unicode standard. –  Malcolm Nov 8 '12 at 2:26
5  
UCS-2 is a technical term, not a buzzword. –  DeadMG Nov 9 '12 at 10:43

For the main part, for the sake of plain and simple future-proofing. Whether it was a misguided reason and the wrong way to go about it is a different question.

You can see some reasons behind some of their design decisions in this document about the 2004 switch to Java 5 and UTF-16, which explains some of the shortcomings as well: Supplementary Characters in the Java Platform, and see Why does the Java ecosystem use different encodings throughout their stack?.

For more details on the pitfalls of using UTF-16, and why UTF-8 is likely to be a better option in general, see Should UTF-16 be considered harmful? and the UTF-8 Everywhere manifesto.

share|improve this answer
6  
+1 for linking to the "Should UTF-16 be considered harmful?" question. I recently discovered the UTF-8 Everywhere manifesto and I believe I am now pretty thoroughly convinced. For what it's worth, although Java got it wrong, I'm pretty convinced that Windows did much much worse. –  Daniel Pryden Nov 7 '12 at 17:32
2  
Well, it's not a surprise that Windows got it more wrong: They made the switch to Unicode earlier, so they had fewer correct choices and less experience. Java got later, got it more right, but still somewhat wrong. Now both have to live with old, incorrect-in-the-general-sense APIs that they have to keep supporting. –  Joachim Sauer Nov 8 '12 at 7:44
1  
That's life in the software world, you have to make choices without having all the data, and when you're wrong you get to live with the consequences for a long time. :-) –  Brian Knoblauch Nov 9 '12 at 14:45
1  
I wonder what the performance implications would have been of making string a "special" type in Java (much like Array is), rather than having String be an "ordinary" class which holds a reference to an "ordinary" array containing the actual characters. Depending upon how a string is generated, UTF-8, UTF-16, or even UTF-32 may be the most efficient way of storing it. I don't think there's any particularly efficient way for an "ordinary" class String to handle multiple formats, but a "special" type with JVM support could. –  supercat Feb 26 at 23:35
    
@supercat: I don't exactly have a precise answer for that, but I've got a related SO answer for that. :) Doesn't really address the special type approach, but discusses the potential gain of having streamlined strings. –  haylem Feb 27 at 8:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.