Saturday, November 1, 2014

String concatenation vs StringBuilder in Java

Lets consider two ways of concatenating Strings in Java.
The most common way to do so is to use operator +:
public String concatenateWithPlus(String s1, String s2){
  return "concatenated=" + s1 + s2;
Another way is to use StringBuilder:
public String concatenateWithStringBuilder(String s1, String s2){
  final StringBuilder result = new StringBuilder();
  return result.toString();
The question is which method is faster and consumes less resources?

It seems natural to think that + operator creates a new instance of a String every time it is applied. However, lets look into the bytecodes generated for these two snippets.
  public java.lang.String concatenateWithPlus(java.lang.String, java.lang.String);
   0: new           #2  // class java/lang/StringBuilder
   3: dup
   4: invokespecial #3  // Method java/lang/StringBuilder."":()V
   7: ldc           #4  // String concatenated=
   9: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  12: aload_1
  13: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  16: aload_2
  17: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  20: invokevirtual #6  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
  23: areturn
  public java.lang.String concatenateWithStringBuilder(java.lang.String, java.lang.String);
   0: new           #2  // class java/lang/StringBuilder
   3: dup
   4: invokespecial #3  // Method java/lang/StringBuilder."":()V
   7: astore_3
   8: aload_3
   9: ldc           #4  // String concatenated=
  11: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  14: pop
  15: aload_3
  16: aload_1
  17: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  20: pop
  21: aload_3
  22: aload_2
  23: invokevirtual #5  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
  26: pop
  27: aload_3
  28: invokevirtual #6  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
  31: areturn
As we see the compiler uses StringBuilder in the bytecode for both cases! So both variants don't create unnecessary String instances, and moreover, the second case requires two more instructions per concatenation (pop and aload_3), therefore is slower than the first one!

Take away from this example is that we should look into the bytecode more often and verify our assumptions with the compiler.

These are the commands to generate bytecode from a Java source:
javap -c MyClass > MyClass.bc

Wednesday, October 29, 2014

Multiple java versions on Mac OS

I like alternatives utility on linux which allows managing multiple versions of the same application. Unfortunately Mac OS does not have such utility therefore one needs to manage dependencies on his own. For java dev such important dependency is obviously java.

It took me a while to understand the directory structure of java versions 6, 7 and 8 which are installed on a newly released Mac OS 10.10 Yosemite.

It is known that Oracle does not support java 6 for Mac OS and instead Apple has its own version:

After installing it or updating to Yosemite the java directory layout looks like that:

$ which java

$ ls -laF /usr/bin/java
lrwxr-xr-x 1 root wheel 77 Oct 29 20:34 /usr/bin/java@ -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java

$ ls -laF /System/Library/Frameworks/JavaVM.framework/Versions
drwxr-xr-x 11 root wheel 374 Oct 29 20:33 ./
drwxr-xr-x 10 root wheel 340 Oct 29 20:20 ../
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4.2@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5.0@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6@ -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6.0@ -> CurrentJDK
drwxr-xr-x 7 root wheel 238 Oct 29 20:20 A/
lrwxr-xr-x 1 root wheel 54 Oct 29 20:27 Current@ -> /System/Library/Frameworks/JavaVM.framework/Versions/A
lrwxr-xr-x 1 root wheel 58 Oct 29 20:33 CurrentJDK@ -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

After installing java 7 and 8 JDK, they land to the following directories:

$ ls -laF /Library/Java/JavaVirtualMachines/
drwxr-xr-x 5 root wheel 170 Oct 29 20:39 ./
drwxrwxr-x 5 root wheel 170 Oct 29 20:20 ../
drwxr-xr-x 3 root wheel 102 Oct 23 21:28 jdk1.7.0_71.jdk/
drwxr-xr-x 3 root wheel 102 Oct 29 20:39 jdk1.8.0_25.jdk/

Note the path difference between /Library/Java/JavaVirtualMachines/ for java 7, 8 and /System/Library/Java/JavaVirtualMachines for java 6

Now we need to make these versions live together and provide flexibility for choosing necessary default java version.
One can do it by following next two steps.

Step 1: Map CurrentJDK to the necessary version

I need java 7 to be default, so

$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

Step 2: Remap /usr/bin/ to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/

I have written a small python2.7 script for migrating binaries to CurrentJDK: remap java.
NOTE: the script requires root privileges

That is it!

For making default another version, we need to repeat the first step. For example, to change to java 8 do:

$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK

$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

Happy coding!

Sunday, March 24, 2013

Create Intellij IDEA launcher on Ubuntu

If you develop using Intellij IDEA on Ubuntu, then it is handy to have launcher on unity panel. There are three relatively easy steps to make it happen:

1. Get current Unity launcher configuration

gsettings get com.canonical.Unity.Launcher favorites


['application://nautilus-home.desktop', 'application://firefox.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']

2. Create IDEA desktop launcher

sudo vim /usr/share/applications/idea.desktop

Copy this content into the file and save it:

[Desktop Entry]
Name=Intellij IDEA 12
Comment=Development IDE
3. Update Unity launcher configuration

gsettings set com.canonical.Unity.Launcher favorites "['application://nautilus-home.desktop', 'application://idea.desktop', 'application://firefox.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']"

Launcher is ready to be used

Thursday, September 27, 2012

Lightweight OS on an old laptop

Since features of modern operating systems require more and more memory, the old laptops with small amount of memory and poor CPU frequency may not be used properly.

In a world of Linux one such example is Ubuntu 12.04. The operating system supports a CPU consuming visual effects, for example, switching between windows.

In such situation an OS with small memory consumption is a must. One such an OS is a lightweight Linux OS named Lubuntu. At the start it consumes around 100M, which allows comfortably programming with such tools as Eclipse and web Browser.

In order to install Lubuntu one may go to their official website and download their distribution. Or if have already installed some OS, such as Ubuntu, you may want to simply install Libuntu desktop with:

sudo apt-get install lubuntu-desktop

Sunday, August 26, 2012

Regular expressions vs shell patterns

Today I have discovered an important difference between regular expressions and shell patterns in a context of file searching in Linux. Their both applications may be found in a powerful utility find. Its arguments -name, -path and -wholename are interpreted as shell patterns, and -regex - as regular expressions.
The difference between regular expressions and shell patterns is in how some characters are interpreted.
Let's consider a star character (*). In shell pattern it is interpreted as match of zero or more characters. In regular expression is interpreted differently: it matches preceding character or group zero or more times.
For example, the string '*tmp' will match all strings ending with tmp by shell pattern processor, but regular expression processor won't match anything at all! In order to write an equivalent regular expression of a shell pattern we have to write '.*tmp'.
These commands are equivalent:
find . -name '*tmp'
find . -regex '.*tmp'
So be careful when writing matching expressions and know how it is interpreted.

Saturday, August 11, 2012

How to set up keyboard layout changing in xfce

One may change keyboard layout in xcfe with the following command:

setxkbmap -option grp:switch,grp:alt_shift_toggle,grp_led:scroll us,de,it

Moreover most users may want to do it only once on login. For this reason the command has to be saved in whether ~/.profile or ~/.bash_profile or ~/.bash_login if one of the two latter files exist. 

Thursday, August 9, 2012

How to enable vim syntax highlighting in Ubuntu 12.04

There are much information on the internet, about how to enable syntax highlighting in Ubuntu.
Some of them advice using the command :syntax=on, another saying about installing vim-full.
These advises are valid for earlier versions of Ubuntu, but not for the 12.04.
So in order to enable syntax highlighting one has to install vim-gnome and it will be enabled automatically:
sudo apt-get install vim-gnome