Monday, April 9, 2012

Java Tutorial & Lesson 4 - Understanding Variables and Data Types

Java Variables and common Data types


OK I have spent longer than I thought in this level (already 4 parts!) but here we will finish learning the concepts of the lesson. Now we can start working with the basics of programming in Java. First let's define some concepts:

Variables:

In computer programming, a variable is a storage location and an associated symbolic name which contains some known or unknown quantity or information, a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represents.
Variables in programming may not directly correspond to the concept of variables in mathematics. The value of a computing variable is not necessarily part of an equation or formula as in mathematics. In computing, a variable may be employed in a repetitive process: assigned a value in one place, then used elsewhere, then reassigned a new value and used again in the same way. (Source)

The way to do this in Java is the following:

int a = 2;
int b = 3;
  
int c = a + b;

Here you are storing the values of numbers in the variables a and b, and then adding the values stored in a and b and saving that result to the variable called c. If you want to test this code, just write those lines of code inside the main method created before, and print the result (println). The result would be like this:

public static void main(String[] args) 
{
    int a = 2;
    int b = 3;
 
    int result = a + b;
 
    System.out.println( "The value now stored in result is: " + result );  
}

Note: Since Java is case sensitive, which means that if you call a variable something like myvariable, and then try to obtain the value stored in it by calling myVariable, you won't get the same result, make sure you keep the same name throughout the code. Again, Eclipse will warn you if you make this mistake, but still you should be careful.

Data Types

In our previous example, we have stored a variable of type int. This means that it can only contain values between -2,147,483,648 (-231) and 2,147,483,647 (231-1). Some of the most common types of data that we can store in a variable are:

Tuesday, April 3, 2012

Java Tutorial & Lesson 3 - Writing the first program: Hello World!

Writing the Hello World in Java


Once you have the environment set up, we can actually begin to code. In last part we set up the environment to work with Java and now we are ready to begin. Here is what you should have by now:



The first line as you can see, says where are we located, this time we are located in a package called "main". There can only be one line that defines the location of the Class for it can only be in one place at the time. The second line defines our class. A class is just a group of functions and attributes (that we will learn about later) all organized in one "file".

Create the main method

Now, for the coding part write inside of the brackets the word "main" and press the space bar while holding the Ctrl key. This is one of my favorite features of Eclipse and it is called Auto-completion. What it does is it gives you a list of possible words of set of words to complete what you are writing, saving you some time while programming. Now you should see something like this:



Select the option that says "main method". If you do it right you should now have something like:

public class Main 
{ 
    public static void main(String[] args) {
    //This is a comment. The contents will be here.
    }
}

This method right here is the first method that will always be executed when running the program as a Java Application. Whatever there is inside those brackets will be the first lines executed by the Java compiler.

Printing a message in the Console

Now, to actually see something working, write the following line inside the main method:

public static void main(String[] args) // This is the main method.
{
    System.out.println( "Hello Java!" ); // Write this line.
}


and Run your program. To do this, Right-click on the main package in your Package explorer (left panel), Run As >> Java Application:

Java Tutorial & Lesson 2 - Setting up a Project on Eclipse

Set up The Eclipse IDE for a Java Project


Once you have installed the required software, open the Eclipse IDE. Once started, it will ask you for a working directory. Select any empty directory and continue.

Now let's get to the programming!

How to create a Java Project

Right-click the Package Explorer (Left Panel of Eclipse) and create a new Java Project:

(Note: if you can't see any of the panels mentioned here, just go to Window>> Show View and select the missing panel. You can also relocate it by dragging and dropping wherever you want)


In the next menu I recommend no using the default location (it's just a personal preference) but instead saving your project to a different folder, this way you can keep all your projects organized: