Hidden code
Overview
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));
prints
'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 = \" !!\";");
System.out.println("System.out.println(_\u200e+_\u200f+_\u200e\u200f);");
prints
String _ = "Hello ";
String _ = "World";
String _ = " !!";
System.out.println(_+_+_);
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 ch = 0; ch < Character.MAX_VALUE; ch++)
if (Character.isJavaIdentifierPart(ch) && !Character.isJavaIdentifierStart(ch))
System.out.printf("%04x <%s>%n", (int) ch, "" + ch);
Related article
Uses for special characters in Java code
This comment has been removed by the author.
ReplyDeletehey, can you please share with me the decode function related to this page:
ReplyDeletehttp://stackoverflow.com/questions/7389252/shorten-an-already-short-string-in-java
thanks
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.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi, 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.
ReplyDeleteWould you please tell me whether the lib can work on AIX or not?
Thanks again and please have a great day!!
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.
ReplyDelete