Tuesday, 29 September 2015

Use of Variables in JAVA

variables-in-java
Variables:
Variable gives the name for the value which is a fundamental unit of storage in JAVA. In memory the values (like 20, 30, “hai”) are stored in a particular location, These values can be referred using variable names. We can assign different values to the variables, We can also change values of the variables at run time.
Example:
int a=10;
a=a+10;
System.out.println(“The value increased by 10”+a);
In the above line ‘a’ is the variable name and assigned to the value 10.
Then it is incremented by 10, after the second line of this code the value of variable a is changed from 10 to 20. So variables can be changed at runtime.
Variable name can be any word in the normal English.
The variables can be in the form of
int india;
int b_tech;
int _network;
int pop_nv;
char count;
It can be combination of alphabets or numbers or underscore, we can also create lengthy variable names but using of large unrelated names are not necessary.
The variables can’t be in the form of
int b sc;
char %hello;
int 0india;
double 2333;
char b-tech;
JAVA does not allow any numbers or special characters other than underscore or blank spaces in variable name.
Program:
import java.io.*;
class sample
{
public static void main(String a[])
{
int number;
double val;
char _letter;
try
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Any Number");
number=Integer.parseInt(br.readLine());
System.out.println("Enter Any Decimal Point Number");
val=Double.parseDouble(br.readLine());
System.out.println("Enter Any Character");
_letter=(char)br.read();
System.out.println("Number : "+number);
System.out.println("value : "+val);
System.out.println("character : "+_letter);
}
catch(IOException e)
{
}
}
}
Different types of variables are declared in this program, Integer (For whole numbers), Double (For decimal point numbers), Character (For single alphabet) are different types of datatypes in Java programming that will be explained in next tutorial. Just concentrate on the names of the variables and scanning & displaying of variables.
In this code while scanning the variable _letter (character type) there is no parsing function because Character is also like String so there is no need to parse it.
“System.out.println("Enter Any Character");
_letter=(char)br.read();”
This example code may consist of some blocks like try, catch which are under the topic of exception handling that was important aspect of JAVA programming, for including BufferedReader class this blocks must present to execute program without errors.
Types of Variables:
In JAVA variables are classified into three types based on the usage and accessing of variables.
1. Local Variables
2. Instance Variables
3. Class or Static Variables
Local Variables:
Local Variables are declared inside methods and constructors.
There is no access modifier is assigned to the local variables.
The local variables should be initially assigned to a value.
like int a=0;
After that we can change this value and print it
int a=0;
a=a+7;
System.out.println(“The value of a: “+a);
Instance Variables:
Instance Variables are declared inside the class and outside the methods or constructors (Methods and constructors are main key terms used inside the class will be discussed later).
In JAVA access modifiers are provides the limit to the others (Like calling a variable outside the class or call variable from other program) we can prevent others access of our data or variables. In Instance variables we can provide access modifiers to the variables.
Example:
public int a;
private char k;
Class or Static Variables:
Class or Static Variables are declared with static keyword inside the class outside the methods or constructors
These static variables can be called using the class name.
Example;
static int i=889;
Note: Concepts like Calling of variables using class name, methods, constructors, Access modifiers will be explained in coming tutorials, now only consider the variable names and their types (Only consider that the In JAVA three types of variables are used and declaring these variables can differ).
Example (Based on types of variables):
import java.io.*;
class vardemo
{
int a=10;/* Instance Variable-declared inside the class outside the method*/
static int b=20;/*Static Variable with static keyboard*/
void method()
{
int c=30;/*Local Variable declared inside the method*/
}
}
This example is a piece of code that represents the concept of variables and methods are similar to that of functions in C. We will learn about the methods later.
Thus the importance of variables is explained in this post, i hope it was useful to you and gain some information about variables in java programming.
In the upcoming tutorials we can learn about methods, access specifier, etc..