Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8355978

javac mishandles supplementary character in character literal

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 25
    • tools
    • None
    • source
    • low
    • Hide
      It is very likely to be very rare for existing programs to depend on this behavior, given the existing behavior is broken.

      JDK 11 javac was rejecting character literals with multi-surrogate characters, while JDK 17 is accepting them, so any potential problem should be restricted only to newer programs.
      Show
      It is very likely to be very rare for existing programs to depend on this behavior, given the existing behavior is broken. JDK 11 javac was rejecting character literals with multi-surrogate characters, while JDK 17 is accepting them, so any potential problem should be restricted only to newer programs.
    • Language construct
    • Implementation

      Summary

      javac will now correctly reject programs that contains character literals containing multi-surrogate characters.

      Problem

      Certain Unicode characters are not representable by a single char, and are represented as two chars (surrogates). In Java, a character literal can only contain one char value, not two. But javac will accept code like:

         char c = '๐Ÿ˜Š';

      javac will only use the first char/surrogate, and ignore the second one, leading to incorrect and surprising behavior.

      Solution

      javac will produce an error when a character literal contains a Unicode character, that cannot be represented by a single char value.

      Specification

      The new behavior is in line with the existing specification. Specifically, JLS 3.10.4 states:

      Character literals can only represent UTF-16 code units (ยง3.1), i.e., they are limited to values from \u0000 to \uffff.

      I.e. the specification does not allow to use a multi-surrogate character in a char literal.

            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Chen Liang, Vicente Arturo Romero Zaldivar
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: