7

Why does this code

 public class Apostrophier
{
    public static String replace(String s)
    {
        return s.replace('\u0092','\u0027');
    }
}

give

'empty character literal'

when I try to compile ?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

2 Answers2

11

The unicode code points in the source file are replaced by the actual character they represents. Since '\u0027' is for ' (apostrophe). So, your return statement is replaced to:

  return s.replace('\u0092',''');  

Note: \u0092 will also be replaced by control character.

So, the 2nd argument is an invalid character literal. You can rather use \' directly.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • You could also say `s.replace('\u0092','\u005c\u0027')` !!! Not that you'd want to. (`\u005c` is a backslash.) – ajb Jan 17 '14 at 17:37
5

Replacing the unicode sequences is a very early step of the compilation process. In particular, it happens before parsing literals. So when it's time to parse the literals, \u0027 has already been replaced with '. Therefore, after the comma, you have ''', which the compiler can't make sense of.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110