Showing posts from September, 2012

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 = \" !!\"

String intern puzzle

A String based puzzle for you. The following program String te = "te", st = "st"; // "test".length(); String username = te + st; username.intern(); System.out.println("String object the same is: "      + (username == "test")); prints under Java 7 update 7. String object the same is: true but uncomment the "test".length(); line, or run with Java 6 and it prints String object the same is: false Can you work out why?