Variables Programs
Below programs will print,
1. Declaring different type of variables.
2. Declaring different variables.
3. Declaring and initialising variables
4. Declaring and initialising variables in a single line
---------------------------------------------------------------------------------------
Program : Declaring different type of variables
Program name : VarDemo1.java
Output :
Declaring different type of variables start
Declaring different type of variables end
class VarDemo1
{
public static void main(String args[])
{
System.out.println(“Declaring different type of variables start”);
byte b;
short s;
long l;
String name;
System.out.println(“Declaring different type of variables end”);
}
}
Compile : javac VarDemo1.java
Run : java VarDemo1
Output :
Declaring different type of variables start
Declaring different type of variables end
---------------------------------------------------------------------------------------
Program : Declaring different variables
Program name : VarDemo2.java
Output :
Same type of multiple variable declaration start
Same type of multiple variable declaration end
class VarDemo2
{
public static void main(String args[])
{
System.out.println(“Same type of multiple variable declaration start”);
System.out.println(“Same type of multiple variable declaration start”);
int x, y, z ;
System.out.println(“Same type of multiple variable declaration end”);
}
}
Compile : javac VarDemo2.java
Run : java VarDemo2
Output :
Same type of multiple variable declaration start
Same type of multiple variable declaration end
---------------------------------------------------------------------------------------
Program : Declaring and initialising variables
Program name : VarDemo3.java
Output :
variables initialization start
a value is : 10
b value is : 20.0
c value is : Nireekshan
variables initialization end
class VarDemo3
{
public static void main(String args[])
{
System.out.println("variables initialisation start");int a;
float b;
String c;
a = 10;
b = 20;
c = "Nireekshan";
System.out.println("a value is : "+a);
System.out.println("b value is : "+b);
System.out.println("c value is : "+c);
System.out.println("variables initialisation end");
}
}
Compile : javac VarDemo3.java
Run : java VarDemo3
Output :
variables initialisation start
a value is : 10
b value is : 20.0
c value is : Nireekshan
variables initialisation end
---------------------------------------------------------------------------------------
int a = 10;
float b = 20;
String c = "Nireekshan";
System.out.println("a value is : "+a);
System.out.println("b value is : "+b);
System.out.println("c value is : "+c);
System.out.println("variables initialisation end");
Program : Declaring and initialising variables in a single line
Program name : VarDemo4.java
Output :
variables initialisation start
a value is : 10
b value is : 20.0
c value is : Nireekshan
variables initialisation end
class VarDemo4
{
public static void main(String args[])
{
System.out.println("variables initialisation start");int a = 10;
float b = 20;
String c = "Nireekshan";
System.out.println("a value is : "+a);
System.out.println("b value is : "+b);
System.out.println("c value is : "+c);
System.out.println("variables initialisation end");
}
}
Compile : javac VarDemo4.java
Run : java VarDemo4
Output :
variables initialisation start
a value is : 10
b value is : 20.0
c value is : Nireekshan
variables initialisation end
---------------------------------------------------------------------------------------
Thanks for your time.
-Nireekshan