Monday, April 16, 2012

Welcome Programmers

Java Programming Tutorials and Lessons

Welcome to this new Java tutorial. I hope you like it and learn to program in Java really fast.

Very soon I will also include some Tutorials and Lessons about C++.

I have decided to start this blog as a way to reinforce what I have learned through the years and share with other people the knowledge. Learning Java is the best way to begin in the Object Oriented Programming world, since it is very intuitive and somehow easy to learn.

If you are wondering why learning Java is an important matter you can go ahead and check this article about the Top 10 Programming Languages and the Top 10 Programming Languages to keep you employed. Java is one of the most used programming languages for enterprise custom-application development, and one of the easiest to dive into the object-oriented-programming.

Finally take a look at this graph:
Programming Language Popularity
These are normalized results taken from several 
different result sets featured

With Java you can also create fast and useful applications for yourself or anyone who needs a specific task to be automatized. I have used Java to create some cool stuff from small games to time-saving applications.

Now here you can find the lessons created until now. I will leave this post on top of the blog so you can continue with the lesson you left the last time:

Don't forget to follow me on Twitter to get the latest updates.

-Java Tutorial & Lesson 1 - Getting Started

Set up the required components to start programming in Java, including the Java Development Kit (JDK) and the Eclipse IDE.

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

Create a Java project to start using the infinite possibilities of the Eclipse IDE for Java.

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

Say Hello with these simple steps and create your first Java program.

-Java Tutorial & Lesson 4 - Understanding Variables and Data Types

Learn about storing information with certain variables depending on the type of that information and then learn how to use it. Some examples are included in this and almost all the following posts.

-Java Tutorial & Lesson 5 - Understanding Methods and Attributes

Create the first methods to group functions that you will reuse in the future. Attributes are very useful for these kind of global functions.
Let your program make the right decision when confronted with a set of possibilities.

Please feel free to comment and ask if you have any questions, your feedback will be greatly appreciated.

Java Tutorial & Lesson 6 - Making decisions with Conditionals

How to use Java conditionals if and switch

Conditionals are a very important part in any program language. Very useful particularly when you have many options and have to let the computer choose one of them (making a decision) before operating. These kind of computer-decisions are called conditional expressions, and they are usually an equality or inequality that can be evaluated to either true or false.

If-else conditionals in Java

For example, an equality evaluation in Java goes like this:

if( a == b ) //This expressions evaluate if a is equals to b.
    System.out.println( "a and b are the same.");
else //If they are not equal, the message inside the "else" will be printed.
    System.out.println( "a and b are different.");

This can also be written like this:

if( a == b )
    System.out.println( "a and b are the same.");
else if( a != b ) //In Java, != is the opposite of ==
    System.out.println( "a and b are different.");

The last two expressions will show the same result. Now, an inequality evaluation in Java goes like this:

Java Tutorial & Lesson 5 - Understanding Methods and Attributes

Understanding Java Methods

In order to create more complex programs that won't simply do some math (like the previous examples) we need to be able to organize out functions. Until now, we could simply write the whole program inside the main method, but that wouldn't be neither organized nor functional.

Then, in order to write more advanced programs and code that we can use over and over again without re-writing it each time, we will need to use Methods. Here is a small definition of the methods:

Java Methods:

In object-oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property that at runtime, they have access to data stored in an instance of the class (or class instance or class object or object) they are associated with and are thereby able to control the state of the instance. (Source)

In practical terms, this means that we can use methods to group a set of operations. Let's try that in our project with a few easy examples:

static int add(int a, int b) 
    return a + b;
public static void main(String[] args) 
    int a = 3;
    int b = 2;
    int result = add( a, b );
    System.out.println( "The answer is: " + result );  

Now we have created a simple method that will simply add two any numbers and return the result. This method's name is add(). The structure of a method is as shown in the example:

  • static: (since it isn't normally used, this will be explained later)
  • int: is the returning type of the method.
  • add: this is the name of the method. You can name it however you want, but preferably something related to what it actually does.
  • (int a, int b): this two elements inside the brackets are called the parameters of the method. It means that in order to make this method work, we have to provide it with two elements (in this case of type int).
Now we can use this method as many time as we want. This is called reusability, which is the ability to perform some operation without writing the whole code again. For example we can write this inside the main method:

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:


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: