tag:blogger.com,1999:blog-34284433974616681672024-02-08T08:10:03.437-08:00TechBlogSome interesting things from a developer point of viewAnonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-3428443397461668167.post-34724738165098684372014-11-01T14:21:00.001-07:002014-11-02T03:17:09.500-08:00String concatenation vs StringBuilder in JavaLets consider two ways of concatenating Strings in Java.<br/>
The most common way to do so is to use operator +:<br/>
<pre class="brush:java;">
public String concatenateWithPlus(String s1, String s2){
return "concatenated=" + s1 + s2;
}
</pre>
Another way is to use StringBuilder:<br/>
<pre class="brush:java;">
public String concatenateWithStringBuilder(String s1, String s2){
final StringBuilder result = new StringBuilder();
result.append("concatenated=");
result.append(s1);
result.append(s2);
return result.toString();
}
</pre>
<b>The question is</b> which method is faster and consumes less resources?<br/><br/>
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.<br/>
<pre class="brush:java;">
public java.lang.String concatenateWithPlus(java.lang.String, java.lang.String);
Code:
0: new #2 // class java/lang/StringBuilder
3: dup
4: invokespecial #3 // Method java/lang/StringBuilder."<init>":()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
</pre>
<pre class="brush:java;">
public java.lang.String concatenateWithStringBuilder(java.lang.String, java.lang.String);
Code:
0: new #2 // class java/lang/StringBuilder
3: dup
4: invokespecial #3 // Method java/lang/StringBuilder."<init>":()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
</pre>
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!<br/><br/>
Take away from this example is that we should look into the bytecode more often and verify our assumptions with the compiler.<br/><br/>
PS:<br/>
These are the commands to generate bytecode from a Java source:<br/>
<pre class="brush:bash">
javac MyClass.java
javap -c MyClass > MyClass.bc
</pre>Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0tag:blogger.com,1999:blog-3428443397461668167.post-52458561947010598832014-10-29T15:45:00.001-07:002014-10-29T22:49:26.168-07:00Multiple java versions on Mac OS I like <a href="http://linux.about.com/library/cmd/blcmdl8_alternatives.htm">alternatives</a> 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.<br/><br/>
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.<br/><br/>
It is known that Oracle does not support java 6 for Mac OS and instead Apple has its own version: http://support.apple.com/kb/dl1572<br/><br/>
After installing it or updating to Yosemite the java directory layout looks like that:<br/><br/>
<code>
$ which java<br/>
/usr/bin/java<br/><br/>
$ ls -laF /usr/bin/java<br/>
lrwxr-xr-x 1 root wheel 77 Oct 29 20:34 /usr/bin/java@ -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java<br/><br/>
$ ls -laF /System/Library/Frameworks/JavaVM.framework/Versions<br/>
drwxr-xr-x 11 root wheel 374 Oct 29 20:33 ./<br/>
drwxr-xr-x 10 root wheel 340 Oct 29 20:20 ../<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4@ -> CurrentJDK<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.4.2@ -> CurrentJDK<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5@ -> CurrentJDK<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.5.0@ -> CurrentJDK<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6@ -> CurrentJDK<br/>
lrwxr-xr-x 1 root wheel 10 Oct 29 20:20 1.6.0@ -> CurrentJDK<br/>
drwxr-xr-x 7 root wheel 238 Oct 29 20:20 A/<br/>
lrwxr-xr-x 1 root wheel 54 Oct 29 20:27 Current@ -> /System/Library/Frameworks/JavaVM.framework/Versions/A<br/>
lrwxr-xr-x 1 root wheel 58 Oct 29 20:33 CurrentJDK@ -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
</code><br/><br/>
After installing java 7 and 8 JDK, they land to the following directories:<br/><br/>
<code>
$ ls -laF /Library/Java/JavaVirtualMachines/<br/>
drwxr-xr-x 5 root wheel 170 Oct 29 20:39 ./<br/>
drwxrwxr-x 5 root wheel 170 Oct 29 20:20 ../<br/>
drwxr-xr-x 3 root wheel 102 Oct 23 21:28 jdk1.7.0_71.jdk/<br/>
drwxr-xr-x 3 root wheel 102 Oct 29 20:39 jdk1.8.0_25.jdk/
</code><br/><br/>
Note the path difference between <code>/Library/Java/JavaVirtualMachines/</code> for java 7, 8 and <code>/System/Library/Java/JavaVirtualMachines</code> for java 6<br/><br/>
Now we need to make these versions live together and provide flexibility for choosing necessary default java version.<br/>
One can do it by following next two steps.<br/><br/>
<b>Step 1: Map CurrentJDK to the necessary version</b><br/><br/>
I need java 7 to be default, so<br/><br/>
<code>
$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK<br/>
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK<br/><br/>
<code>
<b>Step 2: Remap /usr/bin/<binary_name> to /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/<binary_name></b><br/><br/>
I have written a small python2.7 script for migrating binaries to CurrentJDK: <a href="https://github.com/tasyrkin/scripts/blob/master/remap_java.py">remap java</a>.<br/>
<code>python remap_java.py</code><br/>
<b>NOTE: the script requires root privileges</b><br/><br/>
That is it!<br/><br/>
For making default another version, we need to repeat the first step. For example, to change to java 8 do:<br/><br/>
<code>
$ sudo rm -f /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK<br/>
$ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK<br/><br/>
<code>
<code>
$ java -version<br/>
java version "1.8.0_25"<br/>
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)<br/>
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
</code><br/><br/>
Happy coding!<br/>Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0tag:blogger.com,1999:blog-3428443397461668167.post-89703806949460966442013-03-24T09:15:00.000-07:002014-11-01T12:36:09.119-07:00Create Intellij IDEA launcher on UbuntuIf you develop using Intellij IDEA on Ubuntu, then it is handy to have launcher on unity panel. There are <b>three</b> relatively easy steps to make it happen:<br/><br/>
<b>1. Get current Unity launcher configuration</b><br/><br/>
<pre class="brush:java;">
gsettings get com.canonical.Unity.Launcher favorites
</pre><br/>
Output:<br/><br/>
<code>
['application://nautilus-home.desktop', 'application://firefox.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']
</code><br/><br/>
<b>2. Create IDEA desktop launcher</b><br/><br/>
<pre class="brush:java;">
sudo vim /usr/share/applications/idea.desktop
</pre><br/>
Copy this content into the file and save it:<br/><br/>
<pre class="brush:java;">
[Desktop Entry]
Name=Intellij IDEA 12
Comment=Development IDE
Exec=idea
Icon=/opt/idea12.0.4/bin/idea.png
Terminal=false
Type=Application
Categories=Development;IDE;
</pre>
<b>3. Update Unity launcher configuration</b><br/><br/>
<code>
gsettings set com.canonical.Unity.Launcher favorites "['application://nautilus-home.desktop', <b>'application://idea.desktop'</b>, 'application://firefox.desktop', 'unity://running-apps', 'unity://expo-icon', 'unity://devices']"
</code><br/><br/>
Launcher is ready to be used Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com2tag:blogger.com,1999:blog-3428443397461668167.post-66699277745111917792012-09-27T13:54:00.000-07:002012-09-27T13:55:59.587-07:00Lightweight OS on an old laptopSince 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.
<br/>
<br/>
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.
<br/>
<br/>
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.
<br/>
<br/>
In order to install Lubuntu one may go to their official website <a href="http://lubuntu.net/">http://lubuntu.net/</a> and download their distribution.
Or if have already installed some OS, such as Ubuntu, you may want to simply install Libuntu desktop with:
<br/>
<br/>
<code>sudo apt-get install lubuntu-desktop</code>Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0tag:blogger.com,1999:blog-3428443397461668167.post-81520151763633471242012-08-26T13:37:00.003-07:002012-08-26T14:00:43.550-07:00Regular expressions vs shell patternsToday I have discovered an important difference between <a href="http://www.gnu.org/software/findutils/manual/html_node/find_html/Regular-Expressions.html#Regular-Expressions">regular expressions</a> and <a href="http://www.gnu.org/software/findutils/manual/html_node/find_html/Shell-Pattern-Matching.html">shell patterns</a> in a context of file searching in Linux. Their both applications may be found in a powerful utility <i>find</i>. Its arguments -name, -path and -wholename are interpreted as shell patterns, and -regex - as regular expressions.
<br/>
The difference between regular expressions and shell patterns is in how some characters are interpreted.
<br/>
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.
<br/>
For example, the string '*tmp' will match all strings ending with <i>tmp</i> 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'.
<br/>
These commands are equivalent:
<br/>
<code>
find . -name '*tmp'
</code>
<br/>
<code>
find . -regex '.*tmp'
</code>
<br/>
So be careful when writing matching expressions and know how it is interpreted.Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0tag:blogger.com,1999:blog-3428443397461668167.post-89748812335910800002012-08-11T01:30:00.002-07:002012-08-26T13:45:50.256-07:00How to set up keyboard layout changing in xfce<div>
One may change keyboard layout in xcfe with the following command:</div>
<br />
<div>
<code>
setxkbmap -option grp:switch,grp:alt_shift_toggle,grp_led:scroll us,de,it</div>
</code>
<br />
<div>
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. </div>Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0tag:blogger.com,1999:blog-3428443397461668167.post-7090671182455673132012-08-09T15:34:00.000-07:002012-08-26T13:46:21.700-07:00How to enable vim syntax highlighting in Ubuntu 12.04There are much information on the internet, about how to enable syntax highlighting in Ubuntu.<br />
Some of them advice using the command :syntax=on, another saying about installing vim-full.<br />
These advises are valid for earlier versions of Ubuntu, but not for the 12.04.<br />
So in order to enable syntax highlighting one has to install vim-gnome and it will be enabled automatically:<br />
<code>
sudo apt-get install vim-gnome
</code>Anonymoushttp://www.blogger.com/profile/10079482185421355197noreply@blogger.com0