Java For Android

Java for Android is subtly different to vanilla Java. Learn about the differences and what they mean for your code in this Java for Android article. By Darryl Bayliss.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Annotations

Have you noticed those peculiar lines of code above some of the method names in this article?

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
}

Those words prefixed with an @ are Annotations. They provide extra information at a variety of stages of an app. Annotations provide extra information to the compiler at runtime or even to generate extra code.

The most common annotation in Android is @Override, which exists to inform the compiler that a specific method should override one from the super class. If your method doesn’t actually override anything, then the compiler will fail and tell you, providing a nice safety net from any oddities you might have encountered.

Another well-known annotation is @TargetApi. It exists to allow your methods to indicate they are for use on a specific or newer version of Android. Using a method with @TargetApi set to a version higher than the current target of your app will cause the compiler to complain that you’re using functionality that isn’t available for your version of Android.

It’s a complaint, but it’s also a polite warning. You can still run your app despite the warning, but you can pretty much count on an ensuing crash.

When you’re building an Android app, you can almost count on needing these two annotations. There are others, and you can learn about the form the Android API documentation. It’s also possible to create your own annotations to automate specific tasks, generate code and other helpful functions.

A shining example of this is the third party library AndroidAnnotations. It provides a variety of custom annotations that simplify multi-line functions into single-line annotations and other useful features. Take a look at what annotations are available and what they offer to your apps.

Where To Go From Here?

Building an Android app requires time, patience and compiling multiple subjects under one roof. Hopefully this article has shed some light on the inner workings and the tricky relationship between Android and Java.

You’ve had a brief intro into these topics:

  • The Android VM. One of the key ways that Java for Android differs from standard Java is in its compilation process, and its virtual machines.
  • Use of POJOs. Plain old java objects are extensively used within Android to form the basis of model objects.
  • Access Modifiers. These are key to making your code readable and easy to reason about.
  • Interfaces. A hugely important topic in Android, as they are in the wider world of Java. Referring to objects via interfaces rather than concrete class implementations will really help to decouple code and create nicely reusable components.
  • Annotations. The Android-specific Java annotations that you’ll be using right from day 1.

There is no substitute for actually making an app; creating an app and experimenting with some of the concepts demonstrated here, and see what works and what fails.

If you’re new to Java, or want to refresh your memory, you can download a free PDF cheat sheet and quick reference that’ll make writing your first Java much smoother.

The forums, found below, are open to you to discuss this tutorial, ask questions, share ideas and muse on the larger theme of developing with Java for Android. There certainly is a lot to talk about!

The Android robot is reproduced or modified from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Contributors

Over 300 content creators. Join our team.