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:



Data Type Default Value (for fields) Minimum Range Maximum Range
 int  0  -2147483648  +2147483647
 double  0.0d  4.94065645841246544e-324d  1.79769313486231570e+308d
 char  '\u0000' 0  65,535 (Unicode)
 boolean  false  NA  NA

Those are basically the basic Java types that we will use throughout this tutorial. They are the most commonly used because we can achieve many things with them. Some examples are:

Example 1:
int a = 2;
int b = 3;
  
int c = a + b;  // Variable c becomes 5

Example 2:
double a = 5.0;
int b = 2;
  
double c = a / b;  // Variable c becomes 2.5

Example 3:
int num1 = 2;
int num2 = 3;
  
int c = num1 + num2;  // Variable c becomes 5.

Example 4:
char first = 'a';
  
char second = (char)(first + 1);  // Variable second becomes b, since it is the next letter in the Unicode characters.

Example 5:
boolean var1 = true;
boolean var2 = false;
  
boolean result = var1 && var2;  // Variable result becomes false, we will discuss this later.

Now let's try an example. Imagine that you have the information of one of your employees that just arrived from another country to work with you. This information includes: first name, last name, country of origin, and date of recruitment, job position.
Now, you have this information stored in variables, and you want to print a message that will greet him in his new job. The way to do this would be as follows:


public static void main(String[] args) 
{
    //The information of the employee.
    String firstName = "Chuck";
    String lastName = "Norris";
    String country = "Fight Land";

    int startingDay = 16;
    int startingMonth = 03;
    int startingYear = 2012;

    String jobPosition = "Robot Engineer";
  
    //The message to display.
    System.out.println("Welcome to our company Mr "+firstName+" "+lastName+".");
    System.out.println("");
    System.out.println("We are very pleased to receive you from "+country+" on the following date: "+startingDay+"/"+startingMonth+"/"+startingYear+".");
    System.out.println("You will be working as an honorable "+jobPosition+".");
    System.out.println("");
    System.out.println("Regards,");
    System.out.println("The Company.");

}



Finally, if you are interested, here are some other types that we will not use in this tutorial but you will probably use as you become more expert in Java:

Data Type Default Value (for fields) Minimum Range Maximum Range
 byte  0  -128  +127
 short  0  -32768  +32767
 long  0L  -9223372036854775808  +9223372036854775807
 float  0.0f 1.40129846432481707e-45  3.40282346638528860e+38


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

2 comments:

  1. nice lesson, a bit complicated tho, thanks

    ReplyDelete
    Replies
    1. Why did you find it complicated? Let me try to fix it if I can

      Delete