First steps in Java!

Posted on November 10, 2010

0


Java is tricky, verbose and ugly. And an excellent programming language!

I’ve always avoided it, with some kind of subconcious thought that I should use it, know it, that real grown-up programmers program in java.

I’we been working on a music notation solution for some months. Started in AS3, while one of the goals is accomplishing good solutions for browser-based notation display and playback. But I also at the same time want to create desktop editors for the notation stuff. And why not an Eclipse plug-in? That would be really cool…

So I started porting it to java the other day. It’s really interesting, and I learn a lot from it. Here are some experiences:

Eclipse Java IDE is great!

Flash Builder is ok, but not at all as mature as the java editor. Two highlights:

  • Typecast suggestion, when a value doesn’t match the current variable type
  • Great refactoring features – renaming works like a charm! (Not at all stable in Flash Builder 4)

When starting with a new language, a decent IDE helps a lot! Thank you, Eclipse guys!

Java is verbose!

Java doesn’t have an equivalent to php’s var_export() or Flex ObjectUtil.toString() functions. (At least not natively. There might of course be some implementations out there.) Instead it’s expected that the programmer overrides the toString() method with his/her own code. This is from a “best practice” example that I found:

StringBuilder s = new StringBuilder();
String NEW_LINE = System.getProperty("line.separator");
s.append(levelString + this.getClass().getName() + " Object {" + NEW_LINE);
s.append(object property 1...) etc.

Many code characters for some simple string output! Of course, it’s done in a well structured object-oriented way, but still…

Xml in java sucks! (compared to AS3)

Working with xml is a joy in AS3, thanks to the e4x implementation. The possibility to use xml as a native type makes it so intuitive. On the other hand, in native java, you have to rely on DOM, SAX and other solutions. That hurts!
Luckily thers a third part solution, JDOM (http://www.jdom.org/) that makes it a lot simpler. Highly recommended!

// Create a xml document - Java DOM
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element root = xmlDocument.createElement("root");
xmlDocument.appendChild(root);

// Create a xml document - JDOM
Document document = new Document(new Element("root"));

// Create a xml document - AS3
var document:XML = new XML("<root></root>");

Java enums are great!

Example: I need an enum to handle how to flip the musical notes. This should have the values UP, AUTO and DOWN. These should correspond to the ordinal values -1, 0 and 1.
In java, a native enum looks like this, simple and clear:

public enum EnumDirectionUAD {
	UP(-1), AUTO(0), DOWN(1);
	public int ordinal;
	private EnumDirectionUAD(int ordinal) {
		this.ordinal = ordinal;
	}
}

// Grabbing the enum value from a string is as simple as this, using built-in enum method .valueOf(String):
EnumDirectionUAD uad = EnumDirectionUAD .valueOf("AUTO");

AS3 doesn’t have enum types, so there has to be a pseudo-enum using classes with static constants:

public class EnumDirectionUAD {		
	public static const UP:EnumDirectionUAD = new EnumDirectionUAD("UP", -1);
	public static const AUTO:EnumDirectionUAD = new EnumDirectionUAD("AUTO", 0);
	public static const DOWN:EnumDirectionUAD = new EnumDirectionUAD("DOWN", 1);

	public var value:String;
	public var ordinal:int;
	public function EnumDirectionUAD(value:String, ordinal:int) {
		this.value = value;
		this.ordinal = ordinal;
	}
	// need to create the getByValue() method by myself...
	public static function get list():Array {
		return [UP, AUTO, DOWN];
	}
	public static function getByValue(value:String):EnumDirectionUAD {
		for each (var enumType:EnumDirectionUAD in EnumDirectionUAD.list) {
			if (value == enumType.value)
				return enumType;
		}
		return AUTO;
	}		
}

Not ideal, but it works well.
In php, on the other hand, there seems to be no way of creating a decent parameterized pseudo-enum. (Haven’t found one yet, anyhow.) It’s simply not possible to work with objects or arrays as static constants. That sucks!

Advertisements