Train with a Trainee: Week 1 Part C – Introduction to Java

2

Train with a trainee: Introduction to Java, Week 1 part C

First and foremost, and I can’t stress this enough, I am not a developer. I want to be though and that’s what counts. If any of the information provided in this 12 week series is incorrect or can be made simpler, please let me know.

This is the third installment of the course. This section deals with part (4) Introduction to Java.  To keep up with the posts  follow @AndroidSPIN and @openandroidmove on twitter for when a new series is up for you to view and learn from. You can also find the whole series HERE in our Forums that will be updated with each step written.

Introduction to Android and Java.

(1) Installing Android – Completed HERE
(2) Creating Hello World – Completed HERE
(3) Running on Emulator - Completed HERE
(4) Introduction to Java, Variables, Data types, Loops, Conditionals and Operators

The final section in this week’s lesson deals strictly with Java coding basics. For the unfamiliar individual, Android is based off of Java. A little  background on Java so we get a basic understanding of what it is and what it does:  Java is an object-oriented programming language. According to my research it was developed by a man named James Gosling and his team and Sun Microsystems. Java applications are most often compiled into bytecode or class file that is capable of running on any Java Virtual Machine (JVM). It is designed to be simpler than its counter parts such as C or C++. The basic principle is to “Write Once, Run Anywhere.” Anyone that is familiar with rooting an Android phone will remember seeing ‘wipe Davlik cache’. The Davlik Cache is the cache for your Davlik VM which is responsible for running your applications. So to make it easier to understand from my reading, JVM is DavlikVM on your phone.

With a little background out of the way it’s on to more important things, like how to write code. Since this next session is about the basics we won’t be actually writing anything for your device or need the AVD at this time. This lesson is about data types, creating loops, what conditionals are and what operators are. Since this section is rather large I feel we should break it down into multiple sections so as not to overload us with information. I hate reading a 50 page training session. So let’s start off with starting a NEW JAVA PROJECT then move into basic code to print a text on your console window.

We will still be using Eclipse for this lesson. Let’s get started.

Open up Eclipse and go to File > New > Java Project.

In your project name field you can type “Learning” and hit Finish. We now have a learning Java folder on the left hand side of Eclipse. So far so good, but we still don’t have a working file yet. If you remember from our Hello World application we had a Package and a src file. Right now this folder is empty. So we need to manually add these in.

What we want to do first is RIGHT click on the learning java folder and select NEW > Package. It will give the source folder we just need to name it. In the Name field: learning

Now open up the learning java folder and then open up the src folder. You will now see a package named learning. We are almost there. We need to RIGHT click on the package select NEW > CLASS

We need to name it again, to keep things simple name it ‘learning’ and put a check mark on “public static void main(String[] args)” Then select Finish.

Now we have a Java program ready for tinkering. This is where the fun begins and gets a little crazy. The good news is this is the last session for the week. So you have until next week to tinker and get the basics down. I am going to do my very best to make this as simple as possible.

Our first Java application will be just like the Hello World one we did for Android, very basic. You see this identical code on your screen with the exception of the highlighted section. I would recommend typing in the highlighted code. Repetition is a good thing here. You can just copy and paste it if you wish but I have found that it doesn’t always work.  Let’s give this string a try.

package learning;

public class learning {

/**

* @param args

*/

public static void main(String[] args) {

System.out.println(“Androidspin”);

}

}


Once you have it typed in, if it is correct there should be no RED checks anywhere next to the code. I like that Eclipse lets you know that something in your code is not correct and identifies that line for you. As you are typing in the code there will be a red X at the left of the screen, don’t worry about that unless you have written the code and it is still there. Then compare your code to the example code and see what is missing. You will notice other things that Eclipse does to help you out. It’s pretty nice.


Now if you RUN the project you will see our text that was placed inside the quotations listed in the OUTPUT window below your project window. Feel free to play around with it all you want. Anything you place inside the (“ “) field will be displayed in the window.

Alternatively the more lines you add in the more it will display for you. Such as:

package learning;

public class learning {

/**

* @param args

*/

public static void main(String[] args) {

System.out.println(“Androidspin”);

System.out.println(“Your #1 source for everything Android”);

}

}


Pretty simple and easily duplicated. You can spend some time here if you want getting familiar with this process. We will be using it again in the next section.

In an effort to break these into manageable sections for everyone I’m going end this one here to give you all a chance to soak this information up and practice a little. The next post will be up shortly and we will move into variables and using them to calculate equations.

Don’t forget you can catch up on previous posts in our forums HERE.




Share This Article....

About the Author

Stormy Beach
Time for an update. I am the proud father of three, 2, 8, and 11. I finally just moved up from the Samsung Vibrant to the Samsung Galaxy S III...... Let me tell you, it is mind blowing. I love Android, I love tech and I love my family. Not in that order though. I work to hard, play even harder and take care of all that are around me when I can.

2 Responses

  1. [...] we will create a new Java Project. Same as we did to set up our Learning Project. Go to File > New > Java Project. Project name: Learning2. On the left side of  Eclipse you will [...]

  2. [...] we will create a new Java Project. Same as we did to set up our Learning Project. Go to File > New > Java Project. Project name: Learning2. On the left side of  Eclipse you [...]

Leave a Reply

© 2010 AndroidSPIN. All rights reserved.