Get $500+ of the best After Effects files, video templates and music for only $20!
Learn Java for Android Development: Introduction to Java

Learn Java for Android Development: Introduction to Java

Tutorial Details
  • Technology: Java (with Android Focus)
  • Difficulty: Beginner
  • Estimated Completion Time: 45-60 Minutes
This entry is part 1 of 13 in the series Learn Java for Android Development

In this tutorial series, you’ll become familiar with Java, the programming language used to develop Android applications. Our goal is to prepare those already familiar with one programming language, such as PHP or Objective-C, to become comfortable working with the Java programming language and dive into Android app development. In this tutorial, you’ll get a brief introduction to Java fundamentals, including object oriented programming, inheritance and more. If you’re new to Java, or just looking to brush up on the details, then this is the tutorial series for you!

Getting Started

As far as prerequisites go, we’re going to assume you understand how to program (perhaps in PHP, or Visual Basic or C++), but that you are unfamiliar with the specifics of programming in the Java language. We aren’t going to teach you to program; we’re going to provide you with clear examples of commonly used Java language constructs and principles, while pointing out some Android-specific tips and tricks.

What You’ll Need

Technically, you don’t need any tools to complete this tutorial but you will certainly need them to develop Android applications.

To develop Android applications (or any Java applications, for that matter), you need a development environment to write and build applications. Eclipse is a very popular development environment (IDE) for Java and the preferred IDE for Android development. It’s freely available for Windows, Mac, and Linux operating systems.

For complete instructions on how to install Eclipse (including which versions are supported) and the Android SDK, see the Android developer website.

What is Java?

Android applications are developed using the Java language. As of now, that’s really your only option for native applications. Java is a very popular programming language developed by Sun Microsystems (now owned by Oracle). Developed long after C and C++, Java incorporates many of the powerful features of those powerful languages while addressing some of their drawbacks. Still, programming languages are only as powerful as their libraries. These libraries exist to help developers build applications.

Some of the Java’s important core features are:

  • It’s easy to learn and understand
  • It’s designed to be platform-independent and secure, using
    virtual machines
  • It’s object-oriented

Android relies heavily on these Java fundamentals. The Android SDK includes many standard Java libraries (data structure libraries, math libraries, graphics libraries, networking libraries and everything else you could want) as well as special Android libraries that will help you develop awesome Android applications.

Why is Java Easy to Learn?

Java is easy to learn for a variety of reasons. There’s certainly no shortage of Java resources out there to help you learn the language, including websites, tutorials, books, and classes. Java is one of the most widely discussed, taught, and used programming languages on the planet. It’s used for many different types of programming projects, no matter their scale, from web applications to desktop applications to mobile applications.

If you’re coming from a traditional programming background like C or C++, you’ll find Java syntax quite similar. If you’re not, then take comfort in knowing that you’ve chosen one of the easiest languages to learn. You’ll be up and running in no time at all.

Finally, Java is one of the most human-readable languages out there, by which we mean that a person who knows nothing about programming can often look at some Java code and have at least an inkling what it’s doing. Consider the following example:

char character = 'a';
if(character=='a')
{
    doSomething();
} else {
    doSomethingElse();
}

If you simply read the code aloud, you can pretty much tell that this snippet of code is doing. There’s a single letter variable called character. If the character variable equals the letter a, then we do something (call the doSomething() method), otherwise we do something else (by calling the doSomethingElse() method).

Why is Platform Independence Important?

With many programming languages, you need to use a compiler to reduce your code down into machine language that the device can understand. While this is well and good, different devices use different machine languages. This means that you might need to compile your applications for each different device or machine language—in other words, your code isn’t very portable. This is not the case with Java. The Java compilers convert your code from human readable Java source files to something called “bytecode” in the Java world. These are interpreted by a Java Virtual Machine, which operates much like a physical CPU might operate on machine code, to actually execute the compiled code. Although it might seem like this is inefficient, much effort has been put into making this process very fast and efficient. These efforts have paid off in that Java performance in generally second only to C/C++ in common language performance comparisons.

Android applications run in a special virtual machine called the Dalvik VM. While the details of this VM are unimportant to the average developer, it can be helpful to think of the Dalvik VM as a bubble in which your Android application runs, allowing you to not have to worry about whether the device is a Motorola Droid, an HTC Evo, or the latest toaster running Android. You don’t care so long as the device is Dalvik VM friendly—and that’s the device manufacturer’s job to implement, not yours.

Why is Java Secure?

Let’s take this bubble idea a bit further. Because Java applications run within the bubble that is a virtual machine, they are isolated from the underlying device hardware. Therefore, a virtual machine can encapsulate, contain, and manage code execution in a safe manner compared to languages that operate in machine code directly. The Android platform takes things a step further. Each Android application runs on the (Linux-based) operating system using a different user account and in its own instance of the Dalvik VM. Android applications are closely monitored by the operating system and shut down if they don’t play nice (e.g. use too much processing power, become unresponsive, waste resources, etc.). Therefore, it’s important to develop applications that are stable and responsive. Applications can communicate with one another using well-defined protocols.

Compiling Your Code

Like many languages, Java is still a compiled language even though it doesn’t compile all the way down to machine code. This means you, the developer, need to compile your Android projects and package them up to deploy onto devices. The Eclipse development environment (used with the Android Development plug-in) makes this pretty painless. In Eclipse, automatic compilation is often turned on by default. This means that every time you save a project file, Eclipse recompiles the changes for your application package. You immediately see compile errors. Eclipse also interprets Java as you type, providing handy code coloring and formatting as well as showing many types of errors as you go. Often, you can click on the error and have Eclipse automatically fix a typo, or add an import statement, or provide a method stub for you, saving lots of typing.

You can still manually compile your code if you so desire. Within Eclipse, you’ll find the Build settings under the project menu. If you have “Build Automatically” turned on, you can still choose the “Clean…” option that will allow you to do full rebuild of all files. If “Build Automatically” is turned off, “Build All” and “Build Project” menu options are enabled. “Build All” means to build all of the projects in the workspace. You can have many projects in an Eclipse workspace.

The build process, for regular Java projects, results in a file with the extension of JAR – Java ARchive. Android applications take JAR files and package them for deployment on devices as Android PacKage files with an extension .apk. These formats not only include your compiled Java code, but also any other resources, such as strings, images, or sound files, that your application requires to run as well as the Application Manifest file, AndroidManifest.xml. The Android Manifest file is a file required by all Android applications, which you use to define configuration details about your app.

What is an Object Oriented Programming Language?

Okay. Time for a very brief and 20,000 foot view of object oriented programming (OOP). OOP is a programming style or technique that relies upon the definition of data structures called objects. For those new to OOP, an object can be thought of much like a custom data type. For example, you might have a Dog object, which represents the blueprint for a generic dog, with a name, breed, and gender. You could then create different instances of the Dog object to represent specific dogs. Each Dog object must be created by calling its constructor (a method that has the same name as the object itself, and may or may not have parameters for setting initial values). For example, the following Dog objects use a constructor with three parameters (name, breed, gender):

Dog dog1 = new Dog("Lassie", collie, female);
Dog dog2 = new Dog("Fifi", poodle, female);
Dog dog3 = new Dog("Asta", foxterrier, male);

So where is this Dog object defined? Well, here we need to begin defining some of the fundamental building blocks of the Java programming language. A class provides a definition for an object. Therefore, there is a Dog class somewhere—either defined by you or in some library somewhere. Generally speaking, a class will be defined in its own file, with the filename matching the class name (e.g. Dog.java). There are exceptions to this rule, such as classes defined within other classes (when a class is declared within a class, it is generally defined for use within the parent class only as a helper class, and referred to as an inner class).

When you want to reference an object from within another class, you need to include an import statement in the top of your class file, much like you would use a #include statement in a compiled language like C.

A class typically describes the data and behavior of an object. The behavior is defined using class methods. Method is the common term for a subroutine in an OOP language. Many common object classes are defined in shared class libraries like software development kits (SDKs), whereas others are defined by you, the developer, for your own purposes. Software is then built up by using and manipulating object instances in different ways.

Please realize this is a very generalized definition of OOP. There are entire books written on this subject. If you’d like to know more about OOP, here are a few resources you might want to check out:

Note: We use a lot of different terminology in this tutorial. There are multiple ways to refer to a given concept (e.g. superclass vs. parent class), which is confusing to those new to object oriented programming. Different developers use different terms, and so we have tried to mention synonyms where appropriate. Deciding which terms you will use is a personal choice.

Understanding Inheritance

Here is another important Java concept you’ll run into a lot: inheritance. Simply put, inheritance means that Java classes (and therefore objects) can be organized into hierarchies with lower, more specific, classes in the hierarchy inheriting behavior and traits from higher, more generic, classes.

This concept is best illustrated by example. Let’s pretend we are developing a Java application to simulate an aquarium. This aquarium has some fish in it. Therefore, we might define a class to represent a fish. This class, called Fish, could include some data fields (also called attributes, or class member variables) to describe a fish object: species, color and size; as well as some of its behavior in the form of methods (also called subroutines, or functions in procedural languages), like eat(), sleep(), and makeBabyFish().

A special type of method, called a constructor, is used to create and initialize an object; constructors are named the same as their class and may include parameters. The following Fish class has two constructors: one for creating a generic Fish object and another for constructing a specific Fish object with some initial data. You’ll also see that the Fish class has two eat() methods: one for eating something random, and another for eating another fish, which would be represented by another instance of the Fish class:

public class Fish {

    private String mSpecies;
    private String mColor;
    private int mSize;

    Fish() {
        // generic fish
        mSpecies = "unknown";
        mColor = "unknown";
        mSize = 0;
    }

    Fish(String species, String color, int size) {
        mSpecies = species;
        mColor = color;
        mSize = size;
    }
    public void eat() {
        // eat some algae
    };

    public void eat(Fish fishToEat) {
        // eat another fish!
    };

    public void sleep() {
        // sleep
    };

    public void makeBabyFish(Fish fishSpouse, int numBabies) {
        // Make numBabies worth of baby fish with Fish spouse
    };
}

Classes can be organized into hierarchies, where a derived class (or subclass) includes all the features of its parent class (or superclass), but refines and adds to them to define a more specific object using the extends keyword. This is called inheritance.

For example, the Fish class might have two subclasses: FreshwaterFish and SaltwaterFish. These subclasses would have all the features of the Fish class, but could further customize the objects through new attributes and behaviors or modified behaviors from the parent class Fish. For example, the FreshwaterFish class might include information about the type of freshwater environment lived in (e.g. river, lake, pond, or puddle). Similarly, the SaltwaterFish class might customize the makeBabyFish() method such that the fish eats its mate after breeding (as defined in the super class) by using the override mechanism, like this:

public class SaltwaterFish extends Fish
{
    @Override
    public void makeBabyFish(Fish fishSpouse, int numBabies) {
        // call parent method
        super.makeBabyFish(fishSpouse, numBabies);
        // eat mate
        eat(fishSpouse);
    }
}

Organizing Object Behavior with Interfaces

In Java, you can organize object behaviors in what are called interfaces. While a class defines an object, an interface defines some behavior that can be applied to an object. For example, we could define a Swimmer interface that provides a set of methods that are common across all objects that can swim, whether they are fish, otters, or submergible androids. The Swimmer interface might specify four methods: startSwimming(), stopSwimming(), dive(), and surface().

public interface Swimmer
{
    void startSwimming();
    void stopSwimming();
    void dive();
    void surface();
}

A class like Fish could then implement the Swimmer interface (using the implements keyword) and provide implementations of the swimming behavior:

public class Fish implements Swimmer {
    // Provide implementations of the four methods within the Swimmer interface
}

Organizing Classes and Interfaces with Packages

Class hierarchies, such as our fish classes, can then be organized into packages. A package is simply a set of classes and interfaces, bundled together. Developers use namespaces to uniquely name packages. For example, we could use com.mamlambo.aquarium or com.ourclient.project.subproject as our package name to keep track of our fish-related classes.

Wrapping Up

Wow! You’ve just embarked on a crash-course in Java for Android development. We’ve covered a pretty intense amount of material here, so let things settle for a bit before moving on to the next lesson of this tutorial series. In Lesson 2, we switch our focus to the nitty-gritty details of Java syntax.

You’ve only scratched the surface of Java development for Android development. Check out all the other great tutorials on Mobiletuts+ to dive deeper into Java and Android development. Good luck!

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon


Related Tutorials:


Series NavigationLearn Java for Android Development: Java Syntax»

Add Comment

Discussion 42 Comments

  1. Nikunj says:

    With this tutorial you made this look really simple. Great Work ;)

  2. Shyama says:

    Neat . You did make it sound simple. Looking forward to Lesson 2

  3. khr2003 says:

    Thank you very much for this tutorial. It is exactly what I was looking for.

  4. Romeo says:

    Thanks a lot for this Tutorial. I have learned Java few months ago and i hardly search throught books to find one that can explain it “easy”. And i can say that the explanation that is given here is very good.
    Now i’m waiting for the other Lessons. Thanks a lot.

  5. Great article!

    I wish this article would’ve been written a couple of months ago when I started developing for Android. It definitely would’ve made my first contact with Java, OOP and Android a lot easier.

    Keep the good work up.

  6. Chad says:

    WOW! Thanks so much for a great beginners guide to Java and OOP. You have helped to clear up a lot of stuff for me. I did a second read through and it became much more clear. I am anxious to continue this journey of learning Java to develop apps for Android.

  7. Divakar Reddy says:

    I like the way he relates the java with the easiest examples now i got the confidence to develop the android application…………

  8. Max says:

    Thanks for this! You really make it seem easy, and a lot of your examples (and analogies) work well.

  9. Ross says:

    Thank you so much for this tutorial! It is the most straight-forward introduction to Java I have found

  10. Very helpful java tutorial for Android development beginners like me. I would love to read more on Android development from you. Already bookmarked your blog. Thanks for sharing the knowledge.

  11. Darshan patel says:

    great job..:) :) still want complete java to android converson is tht possible!!!! provide tht fast.. :)

  12. orlandroid says:

    Thanks a lot guys for this awesome help. Good work!

  13. Mike Wang says:

    This may sound stupid, but can someone who has actually used this tutorial tell me if it actually teaches you to develop an app, or just java, or is there even a difference? And if someone has actually created an app and sold it, can they tell me how tough it is to sell an app? Thanks.

    • razorcookie says:

      This is a tutorial for basic java.
      After this you will hardly be able to develop selling android apps…

  14. James says:

    Hey, It looks like a good tutorial with total 13 parts. I just wanted to know that is it complete java? I mean after getting all these 13 parts, will i be able to make my first android application? and publish it in android market?
    Willing to hear from you back! Thanks!

  15. BERIL says:

    Its good.

  16. Alexey says:

    Thanks for your time and efforts!

  17. Rajesh says:

    Exactly what I am looking for. Great Job..looking forword for your next lessons..

  18. tommy says:

    This is great thanks! btw, what is void? comes up alot in the examples.

    Thanks loads.
    T

  19. gaurav says:

    so simple………..

  20. MgM says:

    Thanks for the clarity. Looking forward to going through the rest of the tutorials.

  21. Swami says:

    Exactly what I was looking, keep up the work.

  22. Great introduction to JAVA very interesting for newbie

  23. What an excellent Java primer for Android. I’ll refer our Android training clients to this page going forward. Thanks a lot!

  24. Rehan says:

    You have done an amazing job..keep it up.

  25. Ferry says:

    Thanks for nice tutorial :-) Keep up with the good job you are doing :-)

  26. Said Bakr says:

    I have not finished reading of this tutorial yet, but this is the first time that I hear that Java is easy to learn! I hope that, because my past tries to learn Java looked like quiet smoking, I never succeeded to learn it.

  27. sun says:

    That is really great work.

  28. Tipy dárky says:

    Thanks a lot, that´s exactly what I have looked for!

  29. Ralph says:

    Nice tutorial. But I’m unfamiliar with any kind of programming language, can I make use of this tutorial to??

  30. Tripti kumari says:

    Thank you so much.This tutorial is really helpful.

  31. samir says:

    Great tutorial!!!
    thanx for give ur best for us
    go on…

  32. Deepika says:

    hi,

    i have an doubt regarding the coding part in Android plat form.. is the Java program can be installed directly to Android mobiles or separate coding have to be done for the mobile platform or Java coding that runs on the system can be directly ported to the mobile environment.can any one help me?

  33. Ravi says:

    Great work!!!
    Congrats :-)

  34. tanu says:

    i was confused in constructor.thanx a ton to admin.your blog help me to clear out :p

  35. Nizzy says:

    I really enjoyed the tutorial for my first step into the Android world.

    I appreciate your time and afford to create this posting.

  36. Java is the most important part of any mobile application development. it makes the user experience to be better and enjoyable. Cheers to JAVA.

  37. Keir Lavelle says:

    Wow. This tutorial is comprehensive and extremely user-friendly. An absolute must for anyone making the transition from any programming / scripting language onto Java – massive thanks for providing this!

  38. Shmilfke says:

    If I already know Java, can I just start programming for Android or is there a special flavor of Java I need to know?

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.