0

I had copy-pasted that snippet of code with the -> from my Chrome browser into Intellij IDE. I thought they were the same. This is not clear. Both characters look the same ->. Both are 2 chars See screenshot before

java: illegal character: '\u02c2'

character in lambda fails compile

When I deleted the -> and typed it in -> then the compiler accepted it.

See screenshot after:

changing to ->

likejudo
  • 3,396
  • 6
  • 52
  • 107

1 Answers1

2

Welcome to the world of homoglyphs: characters that look the same but are actually different.

There are numerous other examples of homoglyphic characters in Unicode, depending on the fonts that your software uses. Other examples include look-alike letters in different languages (examples), minus versus "long hyphen" characters, straight versus sloping quote characters, normal versus wide or non-breaking space, and so on.

In your case, you appear to have copy and pasted

  • U+02C2 Modifier Letter Left Arrowhead. (˂)
  • U+02C3 Modifier Letter Right Arrowhead. (˃)

characters instead of the standard characters

  • U+003C Less Than. (<)
  • U+003E Greater Than. (>)

Q: What is the difference?

A: The difference is that they mean different things to Java, even if the font that your IDE is using has them looking the same. (They look the same in my browser ...)

Q: How do they / did they get there?

A: In examples like this, they typically get there when some word processing application (or a person such as a copy editor) thinks that the alternate version "looks nicer" in some font than the correct character, and substitutes them.

In other cases, they can be deliberately used to trick people; e.g. by using homoglyphs in URLs to trick people into visiting the wrong website; see Homoglyph attack detection in email phishing

The bottom line is that you need to be careful when copying code from documents and web pages that may have been generated by "word processing" software somewhere along the line.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thank you - this gave me the answer! – likejudo Oct 19 '21 at 12:12
  • Ironically, I asked the same question a few weeks ago and SO members closed and deleted it "why is character '->' different in browser and in IDE?" https://stackoverflow.com/questions/69246755/why-is-character-different-in-browser-and-in-ide – likejudo Oct 19 '21 at 12:18
  • 1
    Well last time you asked, the question was *originally* a typo question. (Yes it was!) You changed it after it was closed. Getting people to reopen a question that morphed from a "why won't my code compile" question to a substantially different one is difficult. To be honest, I almost didn't answer *this* question ... because ... I thought that the Question would be slammed for being about typos. – Stephen C Oct 19 '21 at 12:35
  • since the characters looked the same, I had to ask, 'why won't it compile'. But it is telling that SO members use their "hammer" to close and delete questions that are legitimate. It almost feels like bullying. – likejudo Oct 21 '21 at 00:20
  • 1
    It is about quality control / editorial decisions. Don't take it personally. Remember what the real purpose of StackOverflow is. – Stephen C Oct 21 '21 at 00:35