Understanding Java SE

The Android development environment is essentially a combination of Java and XML. The Android SDK includes a runtime that translates the Java and XML code into a language that the operating system and the individual device can understand.

XML stands for eXtensible Markup Language, and it is very similar to the HTML (HyperText Markup Language) that is used for web site design. In fact, HTML is a subset or implementation of XML. XML is designed to structure data for items that require a predefined data structure, and to define constructs so that the user does not need to create them in more complex Java code. If you have done any work in HTML, you will see a familiar sight in XML, known as tags. These tags are bracketed by < and > characters.

Android works with Java Standard Edition, or Java SE. Java SE was created by Oracle, and is much more powerful than Java Micro Edition (Java ME), which is on most mobile phones. What you need to know about Java is that it is an object-oriented programming (OOP) language. What you need to know about OOP is that it uses modular, self-contained constructs known as objects. Objects, like all programming constructs, are merely abstractions intended to help programmers model certain aspects of the real world in terms of logic and math.

Objects

If you need an example of an object in programming, just look around you for physical objects around the room. Right now, I am sitting in a coffee shop and I am trying to figure out the objects as a computer program would see them. A computer doesn’t necessarily know the importance of any given object, unless it is properly defined. To define these objects around me, I am going to pretend that I am receiving a call from my cell phone, and the person on the other end wants me to describe the room around me. How I define it is really up to me, as long as it makes sense to the person on the other end.

I see a chair, and it is made of wood, with four legs, and let’s say that it is 3 feet (36 inches) tall. In front of me is a table, and it is made of metal. Let’s say the table is 37 inches tall, and only has one leg (due to a wide base on the bottom).

Here is my formula for a chair:

class Chair {
int legs = 4;
int height = 36;
String material = "Wood";
}

Here is a formula for a table:

class Table {
int legs = 1;
int height = 37;
String material = "Metal";
}

In the cases above, the blueprint is set with the single word class, and then the object. Within the curly brackets ({ and }) are the variables that hold the states of the objects. States are the characteristics of the objects. In terms of grammatical parts of speech, think of the objects as the nouns and the characteristics as the adjectives. Whole number data is given the declaration of int, while text uses String. Defaults are set with an equal sign, followed by their value.

Now, as it so happens, I decided to use the same descriptors in both the chair and the table to define them. You might also notice that I could have gone into more detail when describing these particular objects. Let’s go back to my friend on the phone, who wants to know more about the objects around me in the coffee shop. Let’s say he asks, “What are the shapes of the tabletops? Are the chairs pushed in or pulled out?”

I can simply alter my formulas to the following new equations. Here is my formula for a chair:

class Chair {
int legs = 4;
int height = 36;
String material = "Wood";
String position = "Pushed in";
}

Here is a formula for a table:

class Table {
int legs = 1;
int height = 37;
String material = "Metal";
String shape = "Rectangular";
}

Notice that I put a position variable on my chair, as the chair can be “pushed in” or “pulled out.” The table is stable, and doesn’t need this variable. Let’s just say that all the chairs are one shape, and I don’t need a shape for them. This is not the state of all the tables in our coffee shop, which is why I chose Rectangular for this.

You can imagine what my friend on the phone would think if I altered the information in the variables. If I wanted to heighten my table by 3 inches, I could set its int height to 40. I could make my chair look like an octopus if I set int legs = 8. I could even have fun and make my table and chair made of marshmallows with String material = "Marshmallow."

Yes, I am being quite a code magician. Of course, objects are really boring if just left to themselves. An object’s fields, or variables, hold its state. However, it needs some actions associated with it, which is why we use methods: programming routines that operate on the object’s internal states. These methods are the verbs to the objects’ nouns.

Methods

Everything that we have created about our table and chairs are simply default labels. A method will define how these objects act on the variables to define their current operational state. In the case of the chairs, I want them to move back and forth, so people can sit on them.

It’s time to create a method, using the void keyword. The method void means that it doesn’t return anything, but if you want it to return something, such as an object, you put that afterward.

void moveChair (String newPosition) {

You will note the method name with lowercase letters followed by uppercase. This is known as camel case, and it is a normal method-naming convention that begins with a lowercase letter and then goes to uppercase letters to begin words embedded in the method name, like this: thisIsCamelCase. Also note the first (opening) curly bracket ({). We have to follow that up by a closing curly bracket (}), and something in between the two. So let’s add this:

void moveChair (String newPosition) {
position = newPosition;
}

This basically tells us that we are setting the table’s position to that which was passed into the moveChair method.

Let’s say I call this in the program, in order to move the chair back. It is as simple as this:

moveChair(back);

Now, our objects are defined within the class, but they cannot do anything until the user creates an instance of the objects, or instantiates them. Let’s say I want to define the two chairs at my table; I could use this formula here:

Public void onCreate (Bundle savedInstanceState) {
        Super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

Chair chairOne = new Chair();
        Chair chairTwo = new Chair();

chairOne.moveChair(forward);

        chairTwo.moveChair(back);

Were this formula put into a program, we would start with two standard chairs. The new keyword creates that object. We have defined the object Chair, given names to our objects, and set default variable values.

We can use the method thanks to the following code construct:

objectName.methodName(variable);

In the case of our chairs, I have moved one forward, and one back.

Inheritance

Now let us talk about the concept of inheritance. Java supports the development of different types of objects that are more specific in their construction. These more specific objects would be subclassed from the main object. A class that is used for inheritance by a subclass is known as a superclass.

For example, I could create a superclass of objects for our chairs and tables known as Furniture. Each of these chairs and tables would be subclasses, and will inherit whatever the superclass has—for example, number of legs, material, height—but I could add specific things for each individual subclass. For example, the position for the chairs and the shape for the tables.

You can create a subclass from a superclass by using the keyword extends, like so:

Class Chair extends Furniture {insert new fields and methods here}

Interfaces

Certain classes conform to a certain pattern in many Java applications, because the rest of the application should know what to expect of those classes when they are instantiated as objects.

A public interface that the classes present to the application makes their use more predictable and allows the user to use them in places where any class of that pattern is suitable. In the words of programmer Wallace Jackson, who I previously mentioned, “The public interface is a label that tells the application what this class can do, without the application needing to test its capabilities.” Implementing an interface is as easy as using an implements command.

The Package Declaration

Recall when I discussed creating an Android application the concept of a package name. I am going to go into detail about what it is. This is the first line of code in any Android and Java application. This is written with a keyword and declaration method like this:

package application.activity

The package concept is like the folder hierarchy that you use on your own computer, and it organizes its code by functionality. The way Android does it is that it organizes its classes into logical packages, which get imported throughout the program.

Programs need import statements, which use code from elsewhere. The way I understand import statements is like this: if you are writing a book, you will probably need other books as resources to help you. If we look at your Android program as the book you are writing, then the import statements are the other books that you need to make your book complete. Android doesn’t need a whole library, just a few necessary volumes to do certain things.

In the example that we used with the two chairs, we used the code of Public void onCreate (Bundle savedInstanceState).

This cannot be used unless we import Bundle. This is done as follows:

import android.os.Bundle;

An import statement is usually written in the following format:

import platform.functionality.classname;

In the case of import.android.os.Bundle, it’s using the Android platform, with functionality of the os with Bundle, allowing the program to create bundles of variables for convenience and organization.

You can find a whole list of packages on the Android Developers web site at http://developer.android.com/reference/packages.html. Just so you know, the package isn’t the highest level of organization as far as Java is concerned. There is a platform or application programming interface (API). This is a collection of all the core packages for a given language or the packages of a specialized product, like Android.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.111.208