12
String a = "Hello\u200e";
String b = "Hello\u200f";

System.out.println("a = '" + a + "' and b = '" + b + "' are length "
                     + a.length() + " and " + b.length()
                     + ", equals() is " + a.equals(b));

The code in the above code snippet produces the following output.

a = 'Hello‎' and b = 'Hello‏' are length 6 and 6, equals() is false

Although the value of both a and b displayed on the console is Hello‏, a.equals(b) returns false. How?

Lion
  • 18,729
  • 22
  • 80
  • 110
Tiny
  • 27,221
  • 105
  • 339
  • 599

2 Answers2

13

U+200E and U+200F are not printable characters. They're both control characters which dictate how the text should be rendered - either left to right, or right to left.

You won't see these in the terminal, and they shouldn't be equivalent strings.

0x200E ^ 0x200F != 0

Makoto
  • 104,088
  • 27
  • 192
  • 230
9

Because the character sequences are not identical. Just because it appears the same on the console does not mean the objects are identical.

monitorjbl
  • 4,280
  • 3
  • 36
  • 45