Tag Archives: java

Swing dashed line performance

Drawing dashed lines in Swing can be surprisingly slow, and worse, the default implementation seems to be sub-optimal.  Of course, for most apps, this isn’t a big deal, but performance-sensitive apps that draw a lot of dashed lines (or draw them often) can suffer. The first thing to optimize is to only draw the portion […]

Overhead of the modulo operator

While optimizing a number-crunching algorithm, I noticed (almost by accident) that a large amount of time was spent performing modulo/remainder operations (%) used for wrapping around the ends of arrays.  I found that changing i = (i + 1) % n; to i++; while(i >= n) { i -= n; } sped up my code.  […]

Unit testing graphics code

I recently worked on a project that involved heavy use of custom graphics.  Specifically, lots of lines.  The code was sufficiently important and complex that it needed to be unit tested.  However, I had never unit tested drawing code before. A simple strategy would be to paint to an image, then compare the result with […]

Plain TCP beats RMI

I’ve been working on a class project that involves a cluster of worker machines.  Just to throw something quick together, I thought I’d use RMI.  Since I could ensure that every machine was running byte-for-byte identical programs, I didn’t think I’d run into class loading issues.  However, I’ve never had good luck with RMI, and […]

HttpServletRequest cheat sheet

Many HttpServletRequest properties can be confusing, so I decided to create this cheat sheet. (Properties can have similar names, some properties contain delimiters and some don’t, etc.) Let’s say your domain is domain.com, and it points to a router which forwards the request to port 8080 on a server called bob with a local IP […]

The Java DOM API sucks

How it sucks I recently wrote some code to filter nodes from a DOM and had issues with NullPointerExceptions: NodeList nl = doc.getElementsByTagName(“mytag”); for(int i = 0, n = nl.getLength(); i < n; i++) { Element elt = (Element) nl.item(i); if(…) { elt.getParentNode().removeChild(elt); } } The problem was that NodeLists are live, and removing elements […]

Double.equals pitfalls

It turns out that Double.equals is not always the same as == with two double primitives. Case in point: double a = Double.NaN; System.out.println(a == a); Double b = new Double(a); System.out.println(b.equals(b)); This code will print out false and true. What’s going on? Basically, if you look at the Javadoc for the equals method, it […]

Using scripts to hide complexity and only making it worse

I recently had to work with software that is customizable using Jython script. As much as I like both Java and Python, this environment was a nightmare. The problem was the inability to write Python in the Python script. First off was the old version of Python. The script was limited to version 2.1 (8 […]