Why does this code, written backwards, print “Hello World!”

后端 未结 5 1508
有刺的猬
有刺的猬 2020-12-02 04:14

Here is some code that I found on the Internet:

class M‮{public static void main(String[]a‭){System.out.print(new char[]
{\'H\',\'e\',\'l\',\'l\',\'o\',\' \'         


        
5条回答
  •  时光说笑
    2020-12-02 04:21

    This is actually because of Unicode bidirectional support.

    U+202E RIGHT TO LEFT OVERRIDE
    U+202D LEFT TO RIGHT OVERRIDE

    So, those are some tricky characters. They are actually defined for right-to-left language support. The real code is

    class M{public static void main(String[]a){System.out.print(new char[]
        {'H','e','l','l','o',' ','W','o','r','l','d','!'});}}
    

    (got this by pasting into cmd.exe). Hope this answer helps you find out how this works.

提交回复
热议问题