Monthly Archives: January 2013

Some more Android development peculiarities

There is a baked in logging class called, intuitively, Log. For some strange reason the authors chose to name its methods, most of them, with one letter. That is :

Log.d(..); // debug level
Log.e(..); // error level
...

Until you hit

...
Log.wtf(..);
...

This one’s funny and I appreciate their sense of humor as it goes up to renaming WTF to “What a Terrible Failure”. Like we’re so dumb that we can’t figure what they really had in mind :P

Another peculiarity is that in an activity (this akin to a Page / Form / whatever) you have some methods that you can/should override such as onCreate(). Everywhere is stated that the first thing you should write in the overriding method is a call to the super (that is base) class’s method. If it is really all that important and vital why didn’t the class designers go with a template method in first place?

That is a design pattern, that in this case, would go like so :

public class Activity {

    // ...

    private void onCreateInternal(Bundle savedInstanceState) {
        //vital stuff
        onCreate(savedInstanceState);
    }

    protected void onCreate(Bundle savedInstanceState) {
    }

    // ...

}

Take note of another difference between C# and Java : the methods and classes are virtual/unsealed by default. So the onCreate method in my example above is overrideable in any subclass. Also in Java terminology the base class is called the superclass.

Other differences of terminology include :

  • A namespace is called a package
  • By default an import (which is akin to a using directive) imports by default just the specified type and not the whole package (namespace). You need to use a wildcard to import the whole package
  • The closest thing to an assembly is called a JAR (Java ARchive).
  • Eclipse by default is set to autocompile. That is whenever you hit CTRL-S (Save) to a Java file the project is (re)compiled. This sounds terrible but it isn’t! You can’t even notice. Either there is an incremental compilation either the performance of the compiler is incredible.
  • Unlike Visual Studio, Eclipse presents the import block collapsed by default. I wish I had this in VS… Just like a mobile app fan would say “there’s an app for that”, I bet for VS “there’s a plugin for that” :P
  • In Java if you want to call the super (base) class’s constructor from the current class’s constructor you will write “super(…);” in the constructor’s body. When I first saw this I said to myself Fu-Kin-Su-Pa (er… “great”, that is). “Now I have the liberty to call the base/super constructor from wherever inside the constructor I want”. Well, no. It’s either the first line or it isn’t.

Well, I’ll rant more as I go learning Android development.

My goal is to get to know enough so I can do it in Xamarin via C# but first I must understand the underlying things in order to go a level of abstraction above.

In the next episode(s) : I am indebted with a follow-up from my presentation held at RONUA last year.

Java / C# differences part 1 of n

Recently I’ve been toying around with Android development (since I own an Android “smart” phone for over 2 years now) and mobile development is all the rage now. Moreover, I’ve been trying to do new things lately since even the pragmatic programmer guide advises us to learn a new language each year (kind of aggressive if you ask me).

Anyway I will try to show what a C# developer (almost 8 years now) discovers by doing Java development on Eclipse for Android.

For today :

leaving a member of a class without an access modifier defaults to internal instead of private :

// C#
void Test() // private method
{
}
// Java
void test() { // internal method
}

Overriding a method does not require any kind of keyword or special ceremony. You can use the @Override annotation but this is optional. You can get burned this way easily.

// C#
public override bool Equals(object other)
{
    return _id == other._id;
}
// Java; WRONG! DO NOT USE
public bool equals(Person other) {
    return this.id == other.id;
}

//Correct
public bool equals(Object other) {
    return this.id == other.id;
}

Overriding requires that you use the same method signature (that is, the same return type, the same parameter types and order). If you accidentally mistake the signature (Person instead of Object) you will overload instead of overriding with unknown effects.

Read more »

WP Like Button Plugin by Free WordPress Templates