Posts Tagged ‘Basics’

Eclipse 3.6M4 – Tiny nice things

Saturday, December 12th, 2009

I personally believe that little things make a difference. Eclipse 3.6 is going to be great for new (and perhaps also for experienced) plugin and RCP developers.

Especially I like in the Eclipse 3.6M4 release:

-consoleLog included per default in a new launch configuration
RCP with a View

I believe the solution of Bug for -consoleLog will solve the most common problem with getting started for new Plugin / RCP developers. Also the update of the PDE RCP templates will hopefully guide new developers in using commands instead of actions.

The change in Bug for -consoleLog was also a community decision, which is a good example that Eclipse does consider user feedback.

Thanks to all involved parties and the Eclipse community for these changes. :-)

Update Unfortunately Mail Template was not part of M4.

 

Fresh (and free) icons for Eclipse applications

Thursday, November 19th, 2009

I always use the Eclipse icons for my applicatons. Recently one of my users complainted that my icons look “oldisch”….

I quick call for help via twitter returned the following sites which provide free and fantastic looking icons.

http://commons.wikimedia.org/wiki/Crystal_Clear
http://www.famfamfam.com
http://iconlet.com
http://www.iconfinder.net/browse

Thanks to everyone for the links. Hope this helps also others to spice up their applications. ;-)

(Disclaimer: Of course these icons are not specific to Eclipse and could be used in other applications as well)

 

Disassemble Java class files via javap

Monday, November 16th, 2009

Today I received the question how someone could see the Java code for a Java class file. You can disassemble the Java byte code via the command line tool javap.

Lets assume you have this tiny Java class Test.java

package test;

public class Test {

	int number = 5;

	public void sayHello() {
		System.out.println("Hello");
	}
}

Compile this class via javac Test. javaand you receive Java.class

If you you run javap Test you receive the attributes and method signatures.

C:\temp\javaptest>javap Test
Compiled from "Test.java"
public class test.Test extends java.lang.Object{
    int number;
    public test.Test();
    public void sayHello();
}

If you you run javap -C Test you receive the byte-code


C:\temp\javaptest>javap -c Test
Compiled from "Test.java"
public class test.Test extends java.lang.Object{
int number;

public test.Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   iconst_5
   6:   putfield        #2; //Field number:I
   9:   return

public void sayHello();
  Code:
   0:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc     #4; //String Hello
   5:   invokevirtual   #5; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

}

To get the full Java source code you can use the tool jad.

16.11.2008 Updated entry based on comments from Eric and Phil. Thanks!

 

Using Collections.sort and Comparator in Java

Tuesday, August 4th, 2009

Sorting a collection in Java is easy, just use Collections.sort(Collection) to sort your values. For example:

package de.vogella.algorithms.sort.standardjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Simple {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(7);
		list.add(2);
		list.add(1);
		Collections.sort(list);
		for (Integer integer : list) {
			System.out.println(integer);
		}
	}
}

This is possible because Integer implements the Comparable interface. This interface defines the method compare which performs pairwise comparison of the elements and returns -1 if the element is smaller then the compared element, 0 if it is equal and 1 if it is larger.

But what if what to sort differently, e.g. for example in different order. Well, you could just use Collection.reverse(). Or you define your own class with implements the interface Comparator.

package de.vogella.algorithms.sort.standardjava;

import java.util.Comparator;

public class MyIntComparable implements Comparator<Integer>{

	@Override
	public int compare(Integer o1, Integer o2) {
		return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
	}
}
package de.vogella.algorithms.sort.standardjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Simple2 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(7);
		list.add(2);
		list.add(1);
		Collections.sort(list, new MyIntComparable());
		for (Integer integer : list) {
			System.out.println(integer);
		}
	}
}

The nice thing about this approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dataOfBirth you could define different implementations of Comparator and sort the objects according to your needs.

 

Java Performance with Strings and StringBuilder

Sunday, July 19th, 2009

Strings are very frequently used in Java programs. This blog post tries to explain what a programmer needs to consider from a performance point of view. It will also explain in what situations you should use StringBuilder instead of String.

Strings in Java are immutable. If you look at the source code of String you find that java.lang.String has the following properties.

/** The value is used for character storage. */
    private final char value[];

 /** The offset is the first index of the storage that is used. */
    private final int offset;

 /** The count is the number of characters in the String. */
    private final int count;

The array is used to store the values of this String.

As Strings are immutable they can be freely shared. This property is utilized in the method substring(). The method substring will use a reference to the same String and only change the offset and the lenght value for the String. The same string is in this case used several times.

Therefore using substring requires only a constant amount of time (and almost no additional memory) and can be freely used.

The operation concat() (which is called by the + operator) combines two Strings. This method has to copy the characters of the two Strings and therefore takes time and extra space which is propotional to the length of the two strings .

The object StringBuilder has a more effectly way of concatenate Strings. It works similar to the class ArrayList by allocating a predefined array for storing the characters and keeps track of the used space. Every time the space is exceeded then it will extend the available capacity).

Does this means that you always have to use StringBuilder if you are concatening strings?

No. Of course if in your program you combine only a few times String the runtime overhead is normally not relevant for the overall performance.

But of course if you combine frequently strings in your program you should switch to StringBuilder.