Posts

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.prin...

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?

Map interface and equals()

The Map interface cannot have duplicate keys and implies that equals is used to determine this. boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.) The Maps which support this are HashMap LinkedHashMap ConcurrentHashMap Hashtable WeakHashMap The Maps which don't use equals() are IndentityHashMap TreeMap ConcurrentSkipListMap EnumMap All have different reasons as to why they violate the contract but it strikes me as not a great rule that it is as often violated as followed.

Java memes which refuse to die

Also titled; My pet hates in Java coding. There are a number of Java memes which annoy me, partly because they were always a bad idea, but mostly because people still keep picking them up years after there is better alternatives. Using StringBuffer instead of StringBuilder The Javadoc for StringBuffer from 2004 states As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization. Not only is StringBuilder a better choice, the occasions where you could have used a synchronized StringBuffer are so rare, its unlike it was ever a good idea. Say you had the code // run in two threads sb.append(key).append("=").append(value).append(", "); Each append is thread safe, but the lock could be release at any point meaning you c...

Uses for special characters in Java code

Overview Ever wondered how you can write code like this in Java? if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡) Background Underscores has long been using in C like language such as Java to distinguish fields and method names. It is common to see a leading underscore like _field or an underscore in a constant like UPPER_CASE . In Java the $ is also used in class names and accessor method names. The SCJP has notes which state Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! This leads to the question; what other connecting characters are there? What are connecting characters? A connecting character joins two words together. This page lists ten connecting characters U+005F LOW LINE _ view U+203F UNDERTIE ‿ view U+2040 CHARACTER TIE ⁀ view U+2054 INVERTED UNDERTIE ⁔ view U+FE33 PRESENTATION FORM FOR VER...

Odd practices in Java

Overview There are a number of practices in Java which oddly baffle me. Here are but a few. Using -Xmx and -Xms The option -Xmx is widely used to set the maximum memory size. As noted in the Java HotSpot VM Options  Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.  So you would think that such a widely used option would not be non-standard any more.  In fact there is a standard option -mx and similarly -ms . I don't know why these standard options are not use more widely, or even documented. Using NIO for non-blocking IO only Non blocking IO was a new feature of NIO for sockets.  However, the default behaviour for NIO Socket is blocking. Files are only blocking in NIO. NIO2 provides an asynchronous interface, but does it by passing your request off to an ExecutorService (which is cheating ...

My first article in Oracle Magazine

Introduction to JIT COmpliation in Java Hotspot JVM Thank you to my co-author Ben, without whom this would never have happened. :)