Java Basics For Android Development
Java Basics For Android Development
Java Basics For Android Development
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.
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.
int – An integer value, i.e. a whole number (no decimals) that includes zero and negative
numbers.
float – A floating point value that includes as many decimal places as it can hold.
Because the decimal place can change, or float, it’s important to know that these values may
technically be imprecise. When precise decimals are needed, like for currency, we should use
the BigDecimal data type.
boolean – A 1-bit true or false value that can only be in one of those states. The Java
keywords for the values are “true” and “false”, which represent 1 and 0, respectively.
char – A single character, such as the letter “A” or the symbol “#”. Note that lowercase and
uppercase characters are different, so “a” and “A” are two different characters.
String – String data is a bunch of characters strung together to make text, like a banner
strung up at a party.
The first four data types in the list above, int, float, boolean, and char, are primitive
data types, which means that they are relatively simple and straightforward. Other
primitive data types include byte, short, and long. For more information about Java’s
primitive data types, visit the documentation from Oracle.
The “S” in String is capital because String is a more complex data type. Strings
are actually objects, and the naming convention in Java is that object names should
start with a capital letter. An object is different than a primitive data type because it has
more complex properties and methods available to it, whereas the primitive data types
are limited and straightforward. For example, the Stringobject has a method
called length() that tells us how many characters are in the String, but the int data
type does not have any methods like that.
Variables
A variable is basically a container used to hold data. The data itself can be anything
from a simple number to coordinates for a location on a map to a URL for a video on the
web. As we just learned, Java is a statically-typed language, which means that we need
to explicitly declare what type of data a variable is supposed to hold. Let’s take a look at
an example.
The line above is a statement that declares a variable named “title” that holds String, or
text, data. It also assigns the text “Java Basics for Android” to the variable. Let’s
examine each numbered part:
1. The first word in a variable declaration is the data type, which tells us what kind of data the
2. The second word is the name of the variable, which can be anything you want following a
few basic rules. Variable names must not contain any spaces or special characters; they can
only have letters, numbers, and underscores. They must not start with a number, though.
3. The equals sign (=) is an operator, meaning it performs a specific operation for us. This is
example it is assigning the text value on the right (#4) to the variable “title” on the left (#2).
4. The text in green is the String value we are working with. In Java, Strings are surrounded
with double quotes to differentiate them from regular text used in the code.
5. Finally, the last character in this line is the semicolon, which is used to finish this statement.
Semicolons in Java are like periods in sentences: we use them to indicate when we’re done
saying something. Every statement in Java must end with a semicolon (note that a statement
Some other examples of variable declarations using some of the basic data types we
covered are as follows:
Note the “f” at the end of the float value that indicates the number is a floating-point
value. Also note that char values are surrounded with single quotes to differentiate them
from String values, which use double quotes.
Methods
A method is a section of code that we can call from elsewhere in our code, and the
method will perform some action or return some kind of result that we can use. Methods
are used to organize our code into reusable (and understandable) chunks that save us
a lot of time and energy.
02 int numberOfCharacters = 0;
04 return numberOfCharacters;
05 }
Line 1 declares the method named length. The first word declares the visibility of the
method and is often either public or private (though a few other options are available).
“Public” means that it’s publicly available to call from anywhere in our app. “Private”
methods are only available within the class they are defined in (more on classes in little
bit).
The second word in the method is the data type that will be returned. In this case the
method will return a number, or int. If the method will run some code but not return any
kind of data, then the keyword void is used instead of a data type.
Next up is the name of the method. Method names follow roughly the same rules as
variable names: letters, numbers, and underscores, but they cannot start with a number.
Directly after the name we see two parenthesis with nothing in between them. The
parenthesis are required, but when empty like this it means we do not pass any data or
variables when we call the method. If one or more variables (with data types) were
included between the parenthesis, then we would need to pass in appropriate values or
variables inside the parenthesis when we call this method.
Line 1 ends with an opening curly brace, and a matching closing curly brace is on line 5.
In Java, blocks of code, like the code that make up a method, are often surrounded by
curly braces to designate all the code that should be run. In this case it means that all
the lines of code between the curly braces will be run each time we call
the length() method.
The last thing I want to mention from this example is the code on line 4.
The return keyword designates that the variable (or data) on the line after
the returnkeyword is what will be returned to whoever called this function. In this
example we’ve calculated the total number of characters that make up the text of the
String and stored the value in an int variable named numberOfCharacters. This variable
is returned, meaning that the actual number stored in the variable will be the return
value of the length() method.
Calling a Method
To use a method, we need to call it from our code and, if it returns a value, store the
return value somewhere. Here’s an example of calling the length() method:
First we have a String value that has 11 characters (because white space is included).
We call the method on the next line using dot notation, which uses a period after a
Class or variable name to call the method from. We can’t just call it like int textLength
= length(); because in this incorrect example, there is no point of reference for where
the length() method is defined or what text it should be checking. By chaining it to
the sampleText String variable, we are saying to use the length() method defined by
the String class, and to use it on the text data held in the sampleText variable.
In this case the length() method calculates a value of 11 and returns it to this code
where it was called from. We do something with the return value by storing it in a new
int variable named textLength.
Comments
In some of the code samples below I’m going to make notes in the code using
comments. Comments in Java code are lines of code that don’t have any effect on how
the program runs. They are purely informational, meant to help us understand how the
code works or is organized.
Let’s take a look at a class that could be used to represent a simple connection to a
website:
In this example we have one member variable mUrl which represents the URL that this
object is intended to connect to. The lower-case “m” prefix is a convention to
indicate member variable. This helps with scoping issues as we can see in the setUrl()
method where a different variable named “url” is clearly different from the member
variable for URL. We also have two methods, or things this object can do. The first is
called setUrl() and is used to set the private member variable (more on “private” in a
minute), and the second is called connect() and would be where code to establish a
connection to the URL would go.
A simple example demonstrating how a robot could be represented as an object
ACCESS MODIFIERS
For classes, member variables, and methods, we usually want to specify who can
access them. In some cases we want things to be publicly available, meaning that
anyone can see and do things with them. Other times, we may want to keep things
private to protect data from the outside world or accidental changes. The
keywords public, private, and protectedare access modifiers that control who can
access the class, variable, or method.
protected Y Y Y N
private Y N N N
no access modifier Y Y N N
Packages
Classes are often grouped together along some kind of criteria into packages. A
package is simply a collection of related classes and are usually named in a format that
follows reverse domain notation, meaning you take the URL for your website and
reverse it, i.e. teamtreehouse.com becomes com.teamtreehouse.
A Note on Casting
If you’ve worked through Build a Simple Android App or Build a Blog Reader App, or if
you’ve done any Android development at all, then you’ll be familiar with the concept of
“casting”. We often see it when getting Views from a layout in code:
Casting in Java, like the cast to Button in the example above, is done by declaring the
type we are casting to inside parenthesis in front of the method that’s returning an
object. For more details about casting in Java, please visit Oracle’s documentation on
classes and inheritance.
Conditionals
One of the basic building blocks of all programming languages is the power to check a
condition and then do something if the condition is met. In Java this achieved using
the if conditional, which checks if a condition evaluates to boolean
values true or false.
if (name.equals("Bruce Wayne")) {
isBatman = true;
We start with the keyword if, and the condition to test is surrounded by parenthesis. If
the condition is met, then the code inside the curly braces is executed. Java, like many
other languages, uses curly braces to start and finish blocks of code like this. If we want
to take some action if the condition is not met, then we can use the elsekeyword to
specify what code should run, as in the following example:
if (name.equals("Bruce Wayne")) {
isBatman = true;
}
else {
isBatman = false;
Further, we can chain as many conditions together as we want using else if, like this:
if (name.equals("Bruce Wayne")) {
isBatman = true;
isSuperman = true;
else {
isRegularHuman = true;
Extra Credit
Depending on your experience with programming, you may be wondering why the
examples above use the equals() method instead of double-equals, like if (name ==
“Bruce Wayne”). The reason is because in Java, applying the double-equals operator to
String objects does not compare the actual values of the objects, just whether or not the
references are equal, which usually isn’t what we’re looking to compare.
Arrays
Arrays are structures used to organize multiple pieces of data or other variables.
Basically they are structured lists where each piece of data is referenced by its position
in the array.
Android App Drawer as Array
Take this example of an App Drawer from an Android phone. We can think of the app
icons in the drawer as items in an array. The first item in the array, Gmail, is at index 0.
The second, Go SMS Pro, is at index 1, and so on to the last, Camera, at index 4. The
length of this array (or the number of items in it) is 5.
When we declare an array variable, we need to specify the type of the items of data
we’ll be holding in the array. For example, if we’re going to use an array to store a list of
names, then we will need to declare it as a String array (since String is the data type for
text).
String[] appNames;
We can initialize an array in one step or multiple steps. In one step, we simply define all
the elements of the array inside curly braces, like these two examples:
If we want to load data in the array at a later time, the we still need to specify how big
the array will be. (We can’t change the number of items in an array–if we want that we
need to use a different data structure like an ArrayList.) Specific items in the array are
accessed and/or assigned using the index of the array. The index is specified inside
square brackets following the name of the array. Arrays in Java start with an index of
zero, so the first item of an array is at position zero and is referenced like this:
appNames[2] = "Chrome";
appNames[4] = "Camera";
The amount of items an array can hold is known as its length. The Array object has a
property called length that returns this number:
Loops
Looping through collections or sets of things is another basic building block of
programming. For example, let’s say we want to loop through that list of app names
from the Arrays section above and output them to a log. If we did it one by one, the
code would look like this:
Log.d(TAG, appNames[0]);
Log.d(TAG, appNames[1]);
Log.d(TAG, appNames[2]);
Log.d(TAG, appNames[3]);
Log.d(TAG, appNames[4]);
What if we have hundreds of apps? Or what if we’re dealing with millions of records
from a database? What if we run 20 lines of code for each record in the database?
That’s a lot of code to write, and most of it is repeating ourselves, which violates
the “Don’t Repeat Yourself (DRY)” principle.
The solution is to loop through an array (or whatever collection you’re dealing with) to
run code for each item in the collection. Let’s rewrite the code above as a few different
loops.
The “for” Loop
for (int i = 0; i < appNames.length; i++) {
Log.d(TAG, appNames[i]);
Let’s walk through this syntax, because there’s a lot here if you’ve never seen it before.
The basic idea is this: “for (some conditions) do the code inside these curly braces”:
This type of for loop often utilizes a counter variable (named “i” for “index” in our
example above) that keeps track of our position in the collection.
1. initialize statement – The first statement inside the parenthesis sets up the counter variable
or whatever mechanism will be used to keep track of our position in the loop. In our example
the variable “i” is set to 0, the first index of the appNames array.
2. terminate statement – The next statement inside the parenthesis (separated by a semicolon)
tells us when this loop should stop processing and exit to the next line of code after the
closing curly brace. In our example we want to keep going as long as “i” is less than the
number of items in the array. Since there are 5 items in the array, but the last item is at index
4 (because it starts at 0), we want to stop once i surpasses 4. Otherwise we will get
an ArrayIndexOutOfBoundsException.
3. step statement – This last statement (again separated by a semicolon) defines how our
counter will change after each step is executed. In our example we are using a special
01 int i = 0;
03 Log.d(TAG, appNames[i]);
04 i++;
05 }
Note that the three statements inside the for loop are here as well: line 1 is the initialize
statement, line 2 contains the terminate statement, and line 4 is the step statement. The
general syntax is as follows:
while (condition) {
// code to execute
Each type of loop can often be rewritten as another, so basically we can choose
whichever makes the most sense for how we want to loop. Sometimes it’s easier to
write one or the other based on the data we’re working with.
int i = 999;
do {
Log.d(TAG, "Just looking at app names");
Log.d(TAG, name);
The for-each loop replaces the three conditions of the for loop with a simplified
declaration. The right side of the colon is the array or collection to be looped through,
and the left side is a temporary variable used to hold the item in the current iteration of
the loop. So in this example we get a String from each item in the appNames array and
the String is stored in a temporary variable named “name”. We can read this as “for
each name in appNames, do the code in these curly braces”.
The act of raising that little flag is called throwing an exception, and we must catch them
in code in our app. Let’s use an example to illustrate:
02 try {
04 }
05 catch (MalformedURLException e) {
07 }
Okay, so what’s going on with these blocks? Up til now, we’ve seen code that executes
normally in the main thread of the program. If any exceptions are thrown in that type of
code, then they are very serious and the app will crash. But oftentimes there are
methods we want to use that throw less serious exceptions, and we are not allowed to
ignore them. In such cases, we need to put that code inside a try block, like lines 2-4
above. In this example, the URL constructor throws a MalformedURLException if a bad
URL is given as the input parameter. If we don’t catch that exception (or some
superclass of it, like Exception) then our code will not compile.
If we need to check for multiple exceptions, like if we tried to open an HTTP connection
using blogUrl, which throws an IOException, then we can chain catchstatements on
after another, like this:
try {
// some code
catch (MalformedURLException e) {
catch (IOException e) {
*Note that no other code can go between the ending curly brace of one catchstatement
and the catch keyword of the next.
Finally, there is one more block of code that can be chained to this sequence:
the finally block. This is where we put code that we want to execute even if an
exception is thrown. In the example below, we open a connection to a file for writing and
want to make sure we close it no matter what happens. Otherwise that connection might
be open indefinitely:
Writer writer;
try {
catch (IOException e) {
finally {
if (writer != null) {
writer.close();
*Note that the writer variable will be null if an exception was thrown trying to initialize it.
Hence we need to check if it’s null or not, because we only want to close it if it was
initialized.