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 onLangPop.com

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: