Tag Archives: default

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 »