Pages

Wednesday, December 22, 2010

Session 2 - Variables and IO - JAVA Programming Guide Online

In this session, you are going to learn about variables and IO operations. This is fairly easy to understand and once you get hold of it, then whatever comes next, will be easy for you. Variables are like containers of information. However, a variable is specific container of information. For instance, if a variable has been specified to hold numbers, it will only hold numbers. Once defined, variable cannot change its type. Lets get started with this to understand in detail.
Lets design a program that adds two numbers. For this, you will have to ask one number from user, store it somewhere, ask another, store it again somewhere, add the two, and display the result. Fairly easy, isn't it? In order to do this, you will have to define three variables. The first one will hold the first number, second one will hold second number, and the third one will hold the addition of two. In order to make this simple, for now, lets not allow the user to enter decimals. This program will only allow whole numbers. For this, we have to define variables as 'integers'. When we define a variable as integer, it only allows user to type in numbers and not decimals. We can define the variables like this:

int value1;
int value2;
int sum;

From the above, you might have noticed that, in java, the format of defining variables is:
;
Here, since data type is integer, we write 'int'. However, you can have any name as variable name. In this case, in order to make it simpler, variable name is value1. Be sure that you keep the variable name simpler so that it is easy for you to understand later on. 
Notice that every line ends with a semi-colon. In java, to terminate statements, semi-colon is used. This is actually beneficial because you can also write the above statement, in order to save space, as:

        int value1; int value2; int sum;
Observe that in above line, variables value1, value2 and sum have same data type. Hence, they can be defined as:
        int value1, value2, sum;
Assigning values to variables is easy. Values can be assigned to variables while defining them. However, you can also assign values afterwards, like:
        int value1=50, value2, sum; //value assigned while defining variables
        value2=25;                  //value assigned after defining them
Primarily, there are four data types:
1. int
2. byte
3. long
4. float
5. double
6. boolean
7. char
8. string
These all have some advantages/disadvantages of their own. Lets continue with our program. Analyse the lines below:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* /
package bloggerjavatutorials;
import static javax.swing.JOptionPane.*;
/**
*
@author Manthan *
*
/
public class addition {
public static void main(String[] args){
int value1,value2,sum;
value1 = Integer.parseInt(showInputDialog("Type in value1:"));
value2 = Integer.parseInt(showInputDialog("Type in value2:"));
sum = value1 + value2;
showMessageDialog(null,"Total is " + sum);
}
Notice the underlined lines. From top, the first underlined line is statement for importing a java library. JOptionPane helps us with GUI input and output. In order to accept values from user, we need to show an input dialog box to user. In this case, we have to use a built-in library that java provides. Second and third underlined lines are assignment lines. Here, an input dialog is shown to user, in which user inputs the data. Since by default, whatsoever user enters is string, we have to convert it to integer data type. Hence the statement Integer.parseInt is used. This method can convert almost any string to integer. The fourth underlined line is for calculating sum. Here, variable 'sum' is assigned value of addition of 'value1' and 'value2'. Fifth underlined line is for showing the output to user. In this case, the output is in format of string. Notice that I do not have to convert integer to string in this line because java understands this automatically and does it for us.
Ok! Time to compile. Hit Shift + F6 to compile the program. Windows displayed are as follows:


Congratulations, You've finally leant basic about variables. In next session, which I will post soon, will be exclusive for extended knowledge about variables. I will discuss in detail about variables, data types, and their limitations. We will also discuss about types of variables. Till then, practice for perfection!

Ads

Look into BLOG ARCHIVE for more posts.