Variable declaration
- We can declare variables in two ways,
- Single variable declaration
- Same type of Multiple variable declaration in a single line.
- Single variable declaration
- Placing or keeping a single variable without value.
- Here one data type name followed by one variable
Syntax:
datatypename variablename;
datatypename : can be byte, short, int, long, float, double, char,
boolean & any class/interface type
variablename : can be anything
; : it's a symbol of semicolon.
Example:
int a ; → variable declaration
int → datatype
a → variable name
--------------------------------------------------------------------------------------
Program :
Program name : Demo1.java
Output :
Declaring a single variable start
Declaring a single variable end
class Demo1
{
public static void main(String args[])
{
System.out.println("Declaring single variable start");
int i ;
System.out.println("Declaring single variable end");
}
}
Compile : javac Demo1.java
Run : java Demo1
Output :
Declaring single variable start
Declaring single variable end
--------------------------------------------------------------------------------------
2. Same type of Multiple variable declaration in a single line:
● Placing or keeping same type of multiple variables without values in a single line.
● Here one data type name followed by multiple variables.
Syntax:
datatypename variablename1, variablename2, variablename3;
Example :
int x, y, z ;
Here x, y, z variables are int type
--------------------------------------------------------------------------------------
Program : Declaring different variables
Program name : Demo2.java
Output :
Same type of multiple variable declaration start
Same type of multiple variable declaration end
class Demo2
{
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 Demo2.java
Run : java Demo2
Output :
Same type of multiple variable declaration start
Same type of multiple variable declaration end
--------------------------------------------------------------------------------------
Thanks for your time.
Thanks for your time.
Nireekshan