It is the basic storage unit in Java
programs. A variable is declared using three elements, an identifier, a type
(data type) and an initializing value. In every program every variable will
have its own scope and lifespan.
How to
declare a variable?
- General Syntax:
type identifier[=value]
[,identifier[=value]…];
- Analyzing Syntax:
type ----- one of the
primitive types in Java or class name or interface name.
identifier
----- the name given to the variable. The
name by which variable will be
identified.
value -----
it is the value used to initialize variable and the value must be the same type
that of the variable type specified.
Note:
You can declare more than one variable of the same specified type by separating
each identifier by comma.
Lets’ see some fragments of code used to
declare and initialize variables in different ways possible:
int
a=10; //declare and initialize.
int
x,y,z; //declare more than one variable of same type.
char
x=’a’,y=’b’,z=’c’; //declare and initialize more than one variable of same
type.
byte
b=20; //byte variable declared.
double
db=16.27; //double variable declared.
int
a; //declare variable.
a=10;//
initialize variable here.
Example Program for Declare and
initialize variables:
/**
Sample program showing declaring variable
*
*
* @author Jaivanth
*
*/
public class SampleDeclareVariable
{
public static void main(String[] args)
{
int a=10; //declare and initialize
variable.
System.out.println("am declared and
initialized, my value is "+a);
int a1,a2,a3,sum;//declare more than one
variable of same type.
a1=a2=a3=a; //use of assignment operator.
sum=a1+a2+a3;// addition statement.
System.out.println("Sum(a1,a2,a3) is
"+sum);
char ch1='J',ch2='A',ch3='V'; //characters declared
and initialized.
System.out.println("I LOVE CODING
"+ch1+ch2+ch3+ch2);
double db=16.27; //double declared and
initialized.
System.out.println("Hi you are at FM
station"+ch1+ch2+ch3+ch2+db+"!! enjoy coding");
}
}
OUTPUT:
am
declared and initialized, my value is 10
Sum(a1,a2,a3)
is 30
I
LOVE CODING JAVA
Hi
you are at FM stationJAVA16.27!! enjoy coding!!
- Scope and Lifespan of Variable:
In Java scope determines until when the
variable will be visible and can be accessed. Lifespan of the variable is the
period until when the value of variable is available in memory for use.
In Java, the block defines the scope and lifespan of variable. The
main() method in Java is also a block. The scope of variable declared in the
main() method is confined to the main() method itself.
In C/C++ you classify scopes into global and
local. But when it comes to Java this won’t be exactly true. This is because of
the strict object-oriented model of java.
In Java you can define till when a variable
should to visible. Ex: whether variable should be confined to only class, or
variable should be available to subclasses or subclasses in other packages etc.
Because of all this possibilities in Java I stated the above sentence. ” In C/C++ you classify scopes into global and local. But
when it comes to Java this won’t be exactly true”.
In Java, scope of variable is defined by class
and methods.
Example program for scope and lifespan of variable:
/**
*
* Sample
program to show variable scope
* @author Jaivanth
*
*/
public class SampleVariableScope
{
public static void main(String[] args)
{
char ch='J';
int i=0;
if(i==0)// if block
{
char ch1='A';
System.out.println("am if block, i can
see variable 'ch'");
System.out.println("my variable is ch1
"+ch1);
System.out.println("ch is "+ch);
}
//ch1='V'; // ch1 is not visible here
//scope of ch1 is confined to if block
System.out.println("Value of ch is
"+ch);// ch is still
visible.
}
}
OUTPUT:
am if block, i can see variable
'ch'
my variable is ch1 A
ch is J
Value of ch is J
Arrays:
They are
group of variables which are of the same type and referred by the common name.
There
are two classification of arrays:
1.
One-Dimensional arrays
2.
Multi-Dimensional arrays
- One-Dimensional arrays
One-dimensional array is the list of same type
of variables.
To
create an array you must implement following two steps:
1.
Declare array
variable.
2.
Allocate the
memory to hold the array.
Code Fragments are given below to make the
array creation easy to understand:
int
a[]; // declare array.
a[]=new int[10]; //allocation of memory.
Analyzing
code fragments:
int
----- data type of declared array.
a
----- identifier of array.
new
----- keyword used to allocate memory to hold the array content.
[10]
------ size of array.
After
creating a new array all the elements of the array are initialized to zero,
until the array is not assigned with new elements.
We
can combine the array declaration and allocation of memory statements together
into a single statement as shown below:
Int a[]=new int[10];
Example program for the one-dimensional array:
/** program one-dimensional array
*
*
* @author Jaivanth
*
*/
public class SampleOneDArray
{
public static void main(String[] args)
{
int array1[]=new int[10];
int i,k=0;
for(i=0;i<10;i++)// for used assigning
array values
{
array1[i]=k;
k++;
}
System.out.println("Array
elements");
for(i=0;i<10;i++)// for used to retrieve
array values
{
System.out.println(array1[i]);
}
}
}
Output:
Array elements
0
1
2
3
4
5
6
7
8
9
You can
assign array elements in the following ways:
1. Int a[]=new int[5];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
2.
Float
a[]={1.2,1.3,1.4,1.5,1.6};
Size
of array depends on the number of elements assigned to array. Size grows or
decreases on the number of the elements added or removed.
- Multi-Dimensional Array:
They are defined as arrays of arrays. It can
be two, three or more than that in dimension.
The below
code fragment shows how two-dimensional array is created:
int myArray[][]=new int[2][3];
Analyzing the above code fragment:
int
----- data type of array.
myArray
----- identifier of array.
new -----
keyword used to create memory space for
array.
[2][3]
------ sizes of two dimensional array.
Example program for Two-Dimensional array:
/** Program for two-dimensional array
*
* @author Jaivanth
*
*/
public class SampleTwoDArray
{
public static void main(String[] args)
{
int array2[][]=new int[5][10];
int i,j,k=0;
for(i=0;i<5;i++) // array element assignment
{
for(j=0;j<10;j++)
{
array2[i][j]=k;
k++;
}
}
for(i=0;i<5;i++)//retrieve array elements
{
for(j=0;j<10;j++)
{
System.out.print(array2[i][j]+" ");
}
System.out.println();
}
}
}
Output:
0
1 2 3 4 5 6 7 8 9
10
11 12 13 14 15 16 17 18 19
20
21 22 23 24 25 26 27 28 29
30
31 32 33 34 35 36 37 38 39
40
41 42 43 44 45 46 47 48 49