Variable initialization
Single variable initialization:
Definition 1 : Placing or keeping a single variable with value.
Definition 2 : Assigning a value to single variable.
Types of Variables initialization:
Basically there are two ways to initialize the variables
1st way of variable initialization:
● In this way, in first step declaring the variable
● In second step assigning the values to the variable.
Syntax :
datatypename variablename ; //declaring
variablename = value ; //assigning value to variable
Example :
int b ; ---> Declaring
b = 20; ---> assigning value to variable
--------------------------------------------------------------------------------------
Program : Declaring and initialising a single variable
Program name : Demo3.java
Output :
Single variable initialization start
Single variable b value is : 20
Single variable initialization end
{
public static void main(String args[])
{
System.out.println(“Single variable initialization start”);
int b ;
b=20;
System.out.println(“Single variable b value is : ”+b);
System.out.println(“Single variable initialization end”);
}
}
Compile : javac Demo3.java
Run : java Demo3
Output :
Single variable initialization start
Single variable b value is : 20
Single variable initialization end
-------------------------------------------------------------------------------------
2nd way of variable initialization:
In this way, in a single line we can declaring and assigning the value to a variable.
Syntax :
datatypename variablename = value ;
Example : int b= 10 ; ---> Declaring & assigning the value
int → datatypename
b → variable name
10 → its value assigned to b variable
= → it's a equivalent symbol to assign
------------------------------------------------------------------------------------
Program : Declaring and initialising a single variable
Program name : Demo4.java
Output :
Single variable initialization start
Single variable b value is : 20
Single variable initialization end
{
public static void main(String args[])
{
System.out.println(“Single variable initialization start”);
int b =20;
System.out.println(“Single variable b value is : ”+b);
System.out.println(“Single variable initialization end”);
}
}
Compile : javac Demo4.java
Run : java Demo4
Output :
Single variable initialization start
Single variable b value is : 20
Single variable initialization end
-------------------------------------------------------------------------------------
Variable Re initialization:
● Once if you initialize the variable, based on requirement we can reinitialize that variable again,
Example : int i = 10;
i = 20;
-------------------------------------------------------------------------------------
Program : Re initializing a variable
Program name : Demo5.java
Output :
variable re initialization start
re initialized b value is : 30
variable re initialization end
{
public static void main(String args[])
{
System.out.println(“variable re initialization start”);
int a =20;
a = 30;
System.out.println(“re initialized b value is : ”+b);
System.out.println(“variable re initialization end”);
}
}
Compile : javac Demo5.java
Run : java Demo5
Output :
variable re initialization start
re initialized b value is : 30
variable re initialization end
-------------------------------------------------------------------------------------
Thanks for your time.
Nireekshan
|