Hidden code


Sometime ago I came across the issue of invisible characters in Strings.  These can really cause confusion because they are invisible.

    String a = "Hello\u200e";
    String b = "Hello\u200f";
    System.out.println('\'' + a + "' and '" + b + "' are length "

                     + a.length() + " and " + b.length() 
                     + ", equals() is " + a.equals(b));


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

Invisible identifiers

Imagine my reaction to discovering you can use invisible characters in identifiers in Java :P.  These characters cannot appear at the start of identifiers in Java but can be anywhere else.

    System.out.println("String _\u200e = \"Hello \";");
    System.out.println("String _\u200f = \"World\";");
    System.out.println("String _\u200e\u200f = \" !!\";");


String _‎ = "Hello ";
String _‏ = "World";
String _‎‏ = " !!";

which when run prints

Hello World !!

So we have three identifiers which all appear the same because they have different invisible characters in their names !!

Amazingly this code compiles, runs and prints all the characters which can be in an identifier but not start them. The code contains \u202e which really messes with the display of the code.

    for (char c‮h = 0; c‮h < Character.MAX_VALUE; c‮h++)
        if (Character.isJavaIdentifierPart(c‮h) && !Character.isJavaIdentifierStart(c‮h))
            System.out.printf("%04x <%s>%n", (int) c‮h, "" + c‮h);

Related article

Uses for special characters in Java code


  1. This comment has been removed by the author.

  2. hey, can you please share with me the decode function related to this page:


  3. You can always add a comment to the question of StackOverflow. I don't know what you don't know so I suggest adding a question or three there. I often suggest people step through the code in a debugger to see what each line of code does.

  4. This comment has been removed by the author.

  5. Hi, Peter. Thank your very much for your great "Java Thread Affinity" library. I used it well in my project, and recently I need to test the lib on IBM AIX system.

    Would you please tell me whether the lib can work on AIX or not?

    Thanks again and please have a great day!!

  6. The library uses standard posix system calls which all Unixes should support. I don't know of anyone who has tried it on AIX, but its worth a try.


Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues