How to compare Strings in Java

A common mistake Java novices do is using the == operator to compare Strings. This does almost certainly lead to unexpected and unwanted behaviour. There is a simple solution to this problem and a not so simple explanation why it is such a common mistake.

The simple solution is to use String.equals instead of the == operator.

So when you want to know if two String objects hold the same value instead of

1
2
3
4
5
String a = "Test";
String b = "Test";
if (a == b) {
  // Do something
}

just use this:

1
2
3
4
5
String a = "Test";
String b = "Test";
if (a.equals(b)) {
  // Do something
}

But look out for null Strings, == handles null Strings nicely but when you call

1
2
3
4
5
String a = null;
String b = "Test";
if (a.equals(b)) {
  // Do something
}

it will result in a NullPointerException!

Continue reading


EML to PDF Converter

I just finished a new project, an EML to PDF Converting tool which i released for free under the AGPL License to github. This tool allows you to convert your Emails in the EML format to PDF’s, which is necessary for archivability. Since Emails reference external resources such as images they tend to be an impermanent medium. To deal with this they can be converted to a format which is permanent such as PDF.

You can download it for free or check out the source code at github!

Screenshot
Continue reading


How to install a (portable) JDK in Windows without admin rights

It recently happened to me that i was stuck at work on a Windows installation without access to admin privileges. While I could use a bunch of portable apps i could not find a portable JDK. To get a portable JDK without admin privileges in Windows you have to follow three few simple steps.

1. Download

Download the JDK from Oracle (e.g. JDK 8 8u25).

 

2. Extract

Open with 7-Zip

Open the .exe file with 7-Zip. It contains a single file tools.zip, which contains all the files we need.
Extract the tools.zip to the desired JDK directory (e.g. “D:\JavaJDK\”).
Continue reading


Why being a programmer ist great or How to get the current windows wallpaper in c#

Being able to make software is great. Not only because you can make a living from it, but also because it helps you in everyday life. Like when you have to manage your 10k+ wallpaper library. I for once always get baited by these “575 awesome wallpapers you absolutely need” posts and download them right away into my library. Every now and then a black sheep sneaks in but i only see it after it pops up on my Desktop (as if i would review all of these 575 wallpapers).

And here it comes together: as a software developer i have the means to write a small piece of code that gets rid of these ugly wallpapers.

So i hacked this snippet that moves the current wallpaper out of its current folder and into a “reject” folder.
The current wallpaper path can be found in a registry entry “TranscodedImageCache” in “HKCU\Control Panel\Desktop” (at least in Windows 8.1). It’s encoded with unicode though and has to be cleaned a little bit.

Continue reading


String replace with callback in Java (like in JavaScript)

When you use JavaScript a lot you are more or less used to the callback-hell, but i hope you love the benefits of passing functions around as much as i do.

For example when replacing a portion of a string:

1
2
3
"test6test12test".replace(/\d+/g, function(str){
  return parseInt(str) * 2;
});

When doing such things in Java it can be a bit of a hassle, but Lambda-Expressions to the rescue!


Convert a ByteArrayOutputStream into a ByteArrayInputStream

Often while programming you find yourself glueing pre-existing code fragments together. This works out sometimes but even more often it is rather annoying. These days i had to encrypt a pdf and do some other stuff with it in java and therefore put together some libraries i found. One of these libraries offered a method to encrypt the pdf and write the result into a ByteArrayOutputStream. Later then i had to push the encrypted pdf into another method that needed it as a ByteArrayInputStream. First i simply copied the content of the ByteArrayOutputStream as byte array into the ByteArrayInputStream, but this didn’t satisfy me since i knew that the ByteArrayInputStream just needed to be initialized with the internal buffer of the ByteArrayOutputStream. Since this internal buffer is hidden from outside i made up the ByteArrayInOutStream which is a ByteArrayOutputStream that has an additional method that returns a ByteArrayInputStream which is initialized with the internal buffer of the ByteArrayOutputStream. This allows for a instant ‘conversion’ that does not use up more memory than required.

Continue reading