[Android Software Development] tjdwowh:
Day 2:

Lesson #1-3: Dissecting Java:

Today, we will dissect a Java class.

But first... let's talk about Eclipse.

Yesterday, we left off at the welcome screen. You won't ever need to see it again. Just exit out of the tab like so:

Now you will be greeted with the screen you will see every time you open Eclipse: the workbench. Don't worry too much about the different panels. You will learn what each of them do eventually. For now, just check the following things.

1. Eclipse has multiple perspectives suited for different tasks. Check to see that the currently selected perspective is Java. If it is not Java, go to Window > Open Perspective > Other and select Java.

2. Look at the bottom panel. If you see a Console tab, move on. If not, go to Window > Show View > Console. This will display the Console tab.

You are now ready to start programming!

Take a look at the Package Explorer - it's on the left side of the screen. This is where Eclipse will organize all of your projects. If you are ready to start coding, read on!

1. Press File > New > Java Project.

2. Enter a project name of your choice.

3. Press Finish. You should now see the project in your package explorer.

4. Double click on it to expand it. You will see the following:

__________________________________________________

src is where all of your Java code will be placed.

JRE System Library (if your version of Java is not JavaSE - 1.7 like my screenshot, don't worry about it. We won't have compatibility issues) contains pre-written Java code that you can import into your own projects. This is there so programmers like me and you do not have to waste time writing code to perform frequented operations; you can just import it.

__________________________________________________

5. RIGHT-CLICK on src, select New, and create a (Java) Class file. Name it HelloWorld.

(By convention, class names start capitalized, and every subsequent word is capitalized!)

6. Hopefully you will see this screen!

Let's talk quickly about Java syntax, or grammar.

1. The purple words "public class" basically are descriptors of the word HelloWorld that follows. They are declaring that HelloWorld is a class that is public (it can be accessed by other classes).

2. The curly braces define where the class begins and ends. The opening { denotes the beginning of the class and the closing } denotes the end of the class. Your entire class is within these two braces.

So what exactly is a class? First, let us talk about objects (as Java docs describe them).

Objects, in the real world, describe things that have both a state and behavior. For example, your phone's state can be: turned on or turned off while its behavior can be: displaying a game or playing music.

Objects, in programming, are similar. They too have a state and behavior, storing state in fields, also called variables, and behaving via use of methods.

A class, then, is the "blueprint from which individual objects are created." Classes are used to create objects, and classes also define these objects' states and behaviors.

The class we have created together, HelloWorld, is not the best example for what a class is, so here's a pseudo-class that better illustrates what a class:

Let's dissect this class. Pay careful attention to the braces, which have been color coded to demonstrate where each brace's counterpart is.

(Note: Think of these braces like parentheses and brackets in math. They sort of determine the "order of operations" in programming. One misplaced brace can make your code behave strangely! For example: [5(2+1)] = 15. But if you change the placement of the parentheses, [(52+1)] = 53, you get a completely different value.)

The red braces contain the class called Phone. In other words, it contains the blueprint that you use to create Phone objects. This class contains one method called togglePower, which as we have mentioned before is used to express or change the behavior of an object.

The yellow braces denote the beginning and end of the method called togglePower. This method checks the value of the boolean variable "turnedOn" and acts accordingly.

The green braces denote the beginning and end of one conditional if statement, and the blue braces do the same for another conditional if statement.

If you have never programmed before, I know some of these things must be strange to you, so let me explain some of these oddities.

1. Whenever a line begins with //, everything that follows is considered a "comment," meaning that the computer will ignore it. You can use comments to leave a note for yourself (for future reference) or for other programmers( to be able to easily discern the purpose of the following lines of code).

2. The three "statements" that follow the first comment:

All three of these are variables. You may have learned in math that variables are letters or symbols that represent another numerical value.

In programming, a variable can be an integer (negative whole numbers, zero, and positive whole numbers), a boolean (true or false), or String (text), among several others.

As before, the purple text modifies the following word. If we refer to the first statement:

int weight = 0;

This statement is creating an integer variable called "weight" and assigning a value of 0 (using the equal sign - this is called initializing a variable, giving it an initial value).

Refering to the second statement:

boolean turnedOn = false;

This statement is creating a boolean variable called "turnedOn" (by convention, Java variables begin lower case and capitalize every subsequent word, like "firstSecondThirdFourth") and assigning a value of "false."

I'm sure you get the idea and can explain the third statement.

You probably have noticed that each of these statements end with a ;

Well, think of the semicolon as the period in Java. It is another punctuation that will prevent your statements from becoming "run-on statements." As a general rule, whenever you are making a declarative statement, you would use a semicolon. More on this later.

Now one last thing:

There is an equal sign = and there is a double equal sign ==.

The first of these, the assignment operator, assigns the Second item as the value of the First item (i.e. "weight = 0" assigns 0 as the value of weight).

The latter of these is comparitive. It compares the first item with the second item (i.e. "turnedOn == true" DOESN'T assign true as the value of "turnedOn" but rather checks if "turnedOn" IS EQUAL to true).

An alternative explanation can be found here: http://www.javacoffeebreak.com/faq/faq0022.html

_________________

Lesson #1-3.5: A Quick Lesson on Naming:

In Java, you can name classes or variables whatever you want as long as it is not one of those purple words that Java has reserved for its own use (public, static, void, int, etc).

Classes are typically named like so:

ExampleClassName.

Methods and variables are named like so:

exampleMethodName()

and

exampleVariableName.

In the Phone class above, togglePower is just a made-up name.

So until you fill that togglePower method with statements (instrutions), togglePower is just a blank method.

_________________

Lesson #1-4: Writing our first program:

You should have a class called HelloWorld now as shown below:

____________________

[COLOR="blue"]public class HelloWorld {

}[/COLOR]

____________________

We are now going to add a method called the main method to this class. If you do not remember what a method does, scroll up and do a quick review before proceeding.

A main method (the method by the name of main) is the "entry-point" for your program. It will automatically be invoked (or called) when you run the program - in other words, pressing Play will run the main method. Nothing more.

Dissecting the Main Method:

____________________

[COLOR="green"]public static void main(String[] args) {

}[/COLOR]

____________________

1. public, static, void - are Java modifiers.

We will discuss these in detail later, but know that these three words are what we would call Java vocabulary, meaning that these words are already pre-defined in Java and you cannot use them as variable names or method names. The three words here describe what type of method main is, the answer to which is, public, static, and void.

2. Methods, in Java, are indicated like below:

I. methodOne()

This above method is called "methodOne" and requires no arguments, or parameters, for the method to work.

II. methodTwo(type argument)

This above method is called "methodTwo" and requires an argument of type type (which can be int, boolean, etc).

When dealing with a method that requires an argument, at the moment it is invoked (or called), it will ask for an input. It will take this input and use it in the method. I will show you how this works in the following examples.

3. Applying #2 to the main method:

main(String[] args)

We know that the main method requires an argument of type String called args. The [] indicates that this is an array, which you can think of as a table of corresponding x and y values. For now, just know that this is what goes in as the argument of the main method every time. You do not need to understand why or what it means just yet. Trust me.

Here is the main method with its braces:

____________________

[COLOR="green"]public static void main(String[] args) {

}[/COLOR]

____________________

Now are you able to tell me where this main method begins and ends?

It begins where the first brace opens { and ends where the second brace closes }.

Let's now insert this method into the HelloWorld class.

__________________

[COLOR="blue"]public class HelloWorld {[/COLOR][COLOR="green"]
public static void main(String[] args) {

}[/COLOR]
[COLOR="blue"]}[/COLOR]

__________________

If you were to copy this code into eclipse and pressed play, it would compile successfully and run.

The problem is... the main method is empty. It won't do anything yet.

So let's write our first statement:

__________________

[COLOR="red"]System.out.println("Hello XDA!");[/COLOR]

____________________

Pretty basic right? No? Let's dissect it.

1. System refers to a class in the library that I've mentioned before (from which you can import code), also known as the API (Application Programming Interface).

2. The periods between "System" and "out" & "out" and "println" represent hierarchy.

For example:

HelloWorld.main(args);

would invoke the main method in the class HelloWorld.

3. println requires an argument. Inside it, you can type both literals and variables.

You indicate literals with quotes: " " and variable names can just be written.

I will show you an example on this.

(Note: If you want to know more about the System.out.println and how it works at the lowest level, you can refer to this site: http://luckytoilet.wordpress.com/2010/05/21/how-system-out-println-really-works/

But I recommend not doing so until you have a very thorough understanding of Java. )

I know it's frustrating for some of you to be writing code that you don't yet fully understand, but just bear with me for a while. Everything will become clear.

Now we insert the statement into the Main Method to get the full working code:

________________

[COLOR="blue"]public class HelloWorld {[/COLOR]
[COLOR="green"]public static void main(String[] args) {[/COLOR]

[COLOR="red"]System.out.println("Hello XDA!");[/COLOR]

[COLOR="green"]}[/COLOR]
[COLOR="blue"]}[/COLOR]

________________

Copy and paste this into eclipse and press play! You should see:

What's going on here? When you press play, the main method is called. The main method has one statement (System.out...)

which displays "Hello XDA!"

NOTE: To make it easier to tell which brace goes corresponds with which, press Ctrl + Shift + F. This auto-formats your code!