Category: java

  • How to test for the implementation of toString()

    Update: This entry has been crossposted to the transylvania-jug blog. Problem statement: you have some value objects for which you implemented toString() (for debugging purposes) and now you would like to test using a unit test that these implementations exist. Possible solutions: Use reflection to detect the existence of the method: boolean hasToStringViaReflection(Class clazz) {…

  • Comparative book review

    Below is a a short comparative review of tow books about Java concurrency which I’ve read in the last couple of months. Disclaier: the Amazon links are affiliate ones. Java Concurrency in Practice is an interesting book, which should be a must-read for anyone doing concurrent programming in Java (and in these days if you…

  • Java has some surprising amount of dinamism in it

    Not long ago I saw some java code from Simone Tripodi. It generates synchronization wrappers around arbitrary objects at runtime in a typesafe manner with a couple of easy to understand lines of code. The heavy lifting is done by the dynamic proxy mechanism available from Java 1.5 if I recall correctly. The downside is…

  • Java Date objects can mutate, even when read

    Ran into this problem a couple of months ago, when we saw some strange dates in production. So I dug into the Java library sources (thank you Sun for providing those!) and found that Date objects aren’t always “normalized”. Rather, sometimes a “denormalized” value is stored which is later (lazily) normalized. The normalized value isn’t…

  • Putting the eval into Java

    “eval” (short for evaluate) is usually the name given to the method in dynamic languages which makes it possible for the programmer to access the compiler / runtime. Here are a few links to the documentation for the function in different languages: Javascript Perl PHP Python Ruby LUA They are usually used to quickly evaluate…

  • Performance optimization techniques for Java code

    Yesterday I gave a presentation at the Transylvania JUG about using profilers and different techniques which you can use to work around the discovered performance problems. Below you can find the embedded presentation. If you are interested in the code samples (as you most probably are, since a big part of the presentation were demos),…

  • String.intern() – there are better ways

    I don’t want to write a “considered harmful” article (because they are harmful), but after experimenting with different solutions I do have a strong opinion that there almost no reason to use String.intern() in Java. But let us proceed step-by-step. First of all, what does String.intern() do? Go read the Javadoc for it and also…

  • Java import statement gotcha

    There is a lot of debate on the intertubes if one should or shouldn’t use wildcard imports. I’m mostly indifferent to the discussion (mainly because all the package references are resolved compile time – so there is no performance overhead – and because today’s IDE’s contain a lot of smarts to help you figure out…

  • Splitting hairs^H^H^H^H^H strings with Java

    Offtopic: where does ^H come from? (since I too found it only recently) – from the source of all wisdom – Wikipedia :-p Pressing the backspace key on a computer terminal would generate the ASCII code 08, BS or Backspace, which would delete the preceding character. That control code could also be accessed by pressing…

  • Choosing a Java profiler

    Recently I’ve been looking around for a Java profiler (since the two things you need for a successful performance tuning session are good data and clear targets). I’ll share the notes about my findings in the hope that they might be useful for someone. Quick disclaimer: don’t believe everything you read on the Internet! These…