Thursday, 4 June 2015

Learning Java through Android

Introduction

I often get the hankering to toy with a language other than C#. It's always interesting to see what other languages do differently, think about why, and possibly even carry those concepts back to .NET.

Java is a big portion of the industry and also a language I didn't know much about, so I got a hold of the Android development tools and had a bit of a hack.

Here are my initial thoughts!

Android != Java

Though Android code is written in Java, it's an older version of Java, so you miss out on these cool Java 8 features that are actually pretty familiar to a C# developer.

Additionally, most of the code you write for Android is leveraging the Android framework - which is unsurprising given the context - but means that there's limited knowledge that can be transferred to Java development in general.

Java != C#

With all the comparisons drawn between C# and Java, you could be forgiven for thinking you'll be able to hit the ground running with no prior knowledge of Java.

It's fairer to say that you'll hit the ground, stumble a bit, then be up to running speed quicker than you think.

Java is Less Militant

There's an air of flexibility in Java development that doesn't exist in C# which can trip you up if you're not careful.

Overridden methods are a great example. In Java, a method being able to be overridden is opt out, whereas in C# it's opt in - an important distinction.

Here's an override in C#:

public class BaseClass 
{
    // *virtual* says this method can be overridden
    // (opt in)
    public virtual void DoStuff()
    {
    }

    // lack of *virtual* says this method can't be overridden
    public void DoOtherStuff()
    {
    }
}

public class DerivedClass : BaseClass
{
    // If we miss the override keyword, the compiler complains
    // that we need either *override* or *new* to explicitly
    // define our expected behaviour for this method
    public override void DoStuff() 
    { 
        base.DoStuff();
    }
}    

And here's an override in Java:

public class BaseClass 
{
    // lack of *final* says this method can be overridden
    public void DoStuff() 
    { 
    }

    // *final* says this method can't be overridden
    // (opt out)
    public final void DoOtherStuff() 
    { 
    }    
}

public class DerivedClass extends BaseClass
{
    // This override annotation is completely optional.
    // If we don't include it, the method still gets overridden!
    // If we do include it, the compiler will check that there's a
    // method to override.
    @Override
    public void DoStuff() 
    { 
        super.DoStuff();
    }
}    

Conclusion

It may not have all of the wonderful features of C# right now (e.g. async/await), but in its flexibility, Java brings its own flavour to the table and is a fantastic language in its own right.

I personally prefer a strict development environment, but you only have to take a quick look at a Python forum to know that not everybody feels the same way.

Whichever way you lean, I recommend that you give Java a fair chance, it's a great change of pace and good fun too!

No comments:

Post a Comment