Before we start getting really in to Android development in our 31 Days of Android series, we need to go back and take a quick run through of Java.  This series is assuming you have a decent understanding of the Java programming language.  For the most part things will be pretty readable so if you’re familiar with object oriented programming but not specifically Java, you shouldn’t have a problem following along.  If you’re already familiar with Java, this article can probably be skipped.

A bit about Java

Java was created in the early and mid 90’s.  Today it’s “owned” by Oracle (due to their purchase of Sun).  Java was designed as an object-oriented language that would be portable and usable on all sorts of hardware.  To this end, Java is not compiled down to native executable (like C or C++).  Java, instead, is compiled to an intermediate bytecode which is then interpreted by a virtual machine that is built specific to the hardware on the device.  This allows a program you create in Java to be run on x86, ARM, PowerPC, and more.  Furthermore, Java is a very structured and ceremonial language.  If you’ve stuck to primarily functional languages in the past, Java may be a bit to swallow.  if you’ve worked with C#, Java will feel like an old friend (though you’ll probably hate Eclipse). 

Data Types

Java is a strongly statically typed language.  This means that there are some restrictions in place, but they are good, helpful restrictions.  The STRONG aspect means that once you create a variable as an integer, it’s an integer for the life of that variable.  You CANNOT set it to a string or a boolean.  The STATIC part means that when your Java program is compiled, it’s going to type check your code to make sure you’re not attempting to break the STRONG typing.  Java has the same sort of variables that you’re used to if you’ve developed in other languages.  String, int, double, boolean, arrays, etc.  Going into every variable type is beyond the scope of this article, so we won’t do it.  If you’re unfamiliar with the basic data types I mentioned before, I would look into going through a full programming tutorial.  You can find all sorts of resources online to help learn Java programming from the ground up.  One note I will make is regarding the String class.  When comparing strings, doing stringA == stringB compares the reference (do they point to the same spot in memory).  If you want to compare two string values (i.e. “Chris” == “Chris”) then you want to do stringA.equals(stringB).


I don’t have a lot to say about Java operators.  Things here function very similarly to the default behavior in most other languages.  One important note to remember is that operator overloading is not possible in Java. 


If you’re not familiar with classes and-object oriented design, this may be one of the biggest concepts to make sure you understand.  Classes are used very heavily in Java.  In general, all of the code you do will be encapsulated in some sort of class. 

   1:  public class MyClass {
   2:  }

If the class is public then it must be contained in a file with the same name as the class (appended with .java).  Non-public classes can be stored in any .java file, however, for the sake of keeping an understandable project structure, it’s best to create separate files for each class.  Java offers class inheritance but not multiple inheritance (similar to C#).  In addition, it also allows implementation of multiple interfaces.  In Android programming, you’re going to end up using inheritance quite often so being familiar with how it works would be a good idea.  In general, understand that a class (A) that inherits from a base class (B), has access to any of B’s public or protected member variables and methods, but not anything private.  Classes can be public, meaning they can be instantiated from anywhere, final, meaning they can’t be extended or altered, or abstract, meaning they can’t be instantiated but MUST be extended.  Classes can also be protected and private but only if they are defined as being part of another class.


Methods, also known as functions, are blocks of code that can be called from other sources.  Let’s say I’m going to add two numbers together in many different places in my application.  Instead of doing this everywhere I want two numbers added, I might create a method that can do that for me, like so:

   1:  public int AddNumbers(int numberOne, int numberTwo) {
   2:       return numberOne + numberTwo;
   3:  }

Now, instead of doing the math, I can call AddNumbers(…) wherever I need the numbers added.  This is a very simplified explanation but should serve to help understand.  All of your logic and functionality will be done inside of methods.  Using my A & B class examples from above, class A can override methods that are a part of class B. When A does this it can call the same method in B by calling super.methodName().  This is useful when you want to create something with similar functionality as something else but alter it slightly.  An example of this is the Activity class.  You’ll use Activity a lot in Android development, initially just for showing data.  For this you’ll inherit from the Android Activity base class.  Within your class, you’ll override the onCreate method that is found in Activity.  Within that method you’ll call super.onCreate since the base Activity class needs to setup some plumbing for you.  Then after you call that, you’ll continue setting up your UI, initializing values, etc.

Loops and Conditionals

Java features the standard loops and conditionals you’ll know from any other language.  Here we have a typical If Then, Else If, Else block.

   1:  if (this is true) {
   2:       //Do this
   3:  } else if (actually this is true) {
   4:       //Actually do this
   5:  } else {
   6:       //None of those things were true so do this
   7:  }

Furthermore, we have the standard switch statement:

   1:  switch (my variable) {
   2:     case "Test":
   3:       //Do some stuff
   4:       break;
   5:     case "For Realz":
   6:       //    Do some real stuff
   7:       break;
   8:     default:
   9:       //This isn't a test or for realz            
  10:  }

One important thing to know about the Java switch statement is that the case statements MUST use final static variables.  That’s the only way you can use variables as cases.  We also have our While loops:

   1:  do { 
   2:       //Do some stuff
   3:  } while (true);
   5:  while (true) {
   6:       //Do some stuff        
   7:  }

Lastly we have the foreach loop:

   1:  String[] names = { "Chris", "Risner" };
   2:  for (String name : names) {
   3:       //Do something with name
   4:  }

Two last, but very important, concepts for loops are break and continue.  These keywords can be used within your loops to either break out of your loop or to stop the current iteration and go to the next instance in the loop, respectively. 

Memory Management

In Java, memory management is not as much of a concern as it is in a language like C++ where you need to explicitly handle memory.  Java has a garbage collector which handles repossessing blocks of memory that are no longer in use by your program.  Now, just because there is a garbage collector does NOT mean that you can’t still run into memory issues.  You are limited to how much memory you can use and if your application just consumes and consumes memory, your app isn’t likely to perform very well. 

Do I have to develop in Java if I want to do Android?

Lastly, I’d like to touch on programming languages for Android.  Java is by far the most well known and popular approach to developing for Android.  The Android SDK is Java based and is the primary entry point for Android.  However, Android also offers the Native Development Kit (NDK).  The NDK allows you to develop in C and C++ and doesn’t offer the same high level access that the SDK does.  You can write your application in Java as well as using the NDK and use the Java Native Interface to connect the two.  The NDK is good for CPU intensive processes and things you want to have compiled to native code instead of bytecode.  One important thing to consider is that when using the NDK, you no longer have the guarantee that your app will run on any Android device with a Java Virtual Machine.  Your NDK code has to be compiled for the right processor architecture as well.


Hopefully this refresher has given you enough to go off of to start doing some real Android development.  Tomorrow we’ll take a look at our first application.

Chris Risner



 if really need once write, run anywhere, don't use NDK, there are several android devices
shipped with intel developed chipsets


This is a very good point wangfei.  In theory, you could make multiple versions of the APK and specify that ARM devices get the version compiled with the NDK that would target the ARM architecture and then do the same with Intel (again, in theory).

Rupert Russell

Hi Thanks for creating this series of articles.  
I am a little confused by this sentence:

"One important thing to know about the Java switch statement is that the case statements MUST use final static variables."

Why have you put a strike through the word  static   ?

In Java is there a "Final variable"?  

Thanks Rupert


Hi Rupert,

That was struck out because originally in this article I said they had to be final static variables, so something like:     final static int myInt = 5;However, you don't need them to be static.  Something like this would be acceptable:     final int myInt 5;
     switch (5) {
          case myInt:
              //do something;

Leave a Comment