<div dir="ltr">I had a question about the definition of the term 'character' in the JLS, more specifically in section 3.10.4 (I was apparently either confused or looking at a JLS version where the definitions were in 3.10.6 at the time)...<div><br></div><div>We had a long, rambling discussion of the meaning of numerous terms in Java here:</div><div><a href="https://coderanch.com/t/743492/java/Java-Terminology-Charset">https://coderanch.com/t/743492/java/Java-Terminology-Charset</a><br></div><div><br></div><div>Where I thought I was reading contradictory definitions of terms relating to 'character'.</div><div><br></div><div>In that thread I say:</div><div><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">I was confused about Java String Literals because of this confusing issue seen in 3.10.6 of the JLS:</span><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><tt style="border:0px;margin:0px;padding:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;font-size:14.2188px;line-height:inherit;vertical-align:baseline;box-sizing:border-box;color:rgb(0,0,0);background-color:rgb(241,232,214)">UnicodeInputCharacter:<br style="box-sizing:border-box">   UnicodeEscape<br style="box-sizing:border-box">   RawInputCharacter<br style="box-sizing:border-box">UnicodeEscape:<br style="box-sizing:border-box">   \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit<br style="box-sizing:border-box">RawInputCharacter:<br style="box-sizing:border-box">   any Unicode character</tt><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"></span><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">So, a "RawInputCharacter" can apparently be *any* valid Unicode character, but I had already seen that UnicodeEscape must be between \u0000 and \uFFFF, requiring surrogate pair encoding by hand for anything outside the BMP.  So String literals are sort of Unicode, but if you are using escape sequences to represent anything, you must know that it is encoded as UTF-16 internally.  I kept hitting that.   </span><br></div><div><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><br></span></div><div>I bring up similar stuff unsuccessfully a few more times, then mentioned this:<br></div><div><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">Specifically the following should all map neatly, but they lose me at the end of the chain, which was true in UCS-2 and as far as I can tell, unless I am confused, this isn't true since they started supporting UTF-16:</span><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">A character literal can be specified as a </span><tt style="border:0px;margin:0px;padding:0px;font-variant-numeric:inherit;font-variant-east-asian:inherit;font-stretch:inherit;font-size:14.2188px;line-height:inherit;vertical-align:baseline;box-sizing:border-box;color:rgb(0,0,0);background-color:rgb(241,232,214)">' SingleCharacter '</tt><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"> which is any 'InputCharacter' except ' or \ (so far so good) which is any 'UnicodeInputCharacter' except CR or LF (I'm still nervously with them) which is any UnicodeEscape or RawInputCharacter (getting a little shaky, as UnicodeEscape is limited to either BMP or at least BMP or a single code unit of a UTF-16 surrogate pair, I am not sure which but let's keep going)...</span><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">lastly they say a RawInputCharacter is "any Unicode character" and I am out, I fold, because we have just proved that 1 == 2.</span><br style="box-sizing:border-box;color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)">You can not shove a non-BMP Unicode character into a Java character literal, as much as I'd like to.  It won't fit.  I am not sure if it is legal or not to stick half of a surrogate pair encoding into a character literal, I see times I think I'd need to, but I know I can't stick both of them in there, it is literally like assigning an int to a short...</span><br></div><div><span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><br></span></div><div>I almost regretted bringing it up at all, but I wound up learning a LOT from that thread, but are the terms consistent and I am missing something, or does the JLS instead rather state that a character literal can be 'any Unicode character' which is untrue, as only BMP characters can be directly represented in a Java character literal?<span style="color:rgb(0,0,0);font-family:"Droid Sans",sans-serif;font-size:14px;background-color:rgb(241,232,214)"><br></span></div><div><br></div><div>I do know the following for sure:</div><div>A character literal is always of type char<br></div><div>No non-BMP Unicode characters can be represented by a single value of type char.</div><div><br></div><div>So how could a character literal ever represent arbitrary non-BMP Unicode characters?  If it can't, then what is the meaning of "Any Unicode Character" within the JLS?  I use the term to mean the union of all BMP and all non-BMP Unicode characters.</div><div><br></div><div>Yours in Confusion,</div><div>Jesse V. Silverman</div></div>