1.4 Variables in Java
Variable in Java -> To strore information.
Declaration: declare the data type with identifier.
DataType varName;
Initialization: intialize the value with identifier
variableName = value;
There are 8 data type:
1. byte
short -> used to store numeric integer.
int
long
2. float
double -> used to store numeric floating point.
3. char -> used to store character.
boolean -> used to store true or false.
eg:- Declaration:
DataType varName;
int empId;
double empSal;
char empGrade;
eg:- Initialization
variableName = value;
empId = 18331;
empSal = 6578.95;
empGrade = 'A';
Example Program 1:
class Program1
{
public static void main(String args[])
{
//variable declaration
int empId;
double empSal;
char empGrade;
//variable initialization
empId = 18331;
empSal = 6578.95;
empGrade = 'A';
//print value of variable
System.out.println("Employee id is="+empId);
System.out.println("Employee Salary is="+empSal);
System.out.println("Employee Grade is="+empGrade);
}
}
Example Program 2:
class Program2
{
public static void main(String args[])
{
int x = 12;
System.out.print("x value is ="+x);
x = 34; //re-assign or re-initialization
System.out.print("x value is ="+x);
x = 45; //re-assign or re-initialization
System.out.print("x value is ="+x);
}
}
Example Program 3:
class Program3
{
public static void main(String args[])
{
int n1 =12;
int n2 = 34;
int n3 = 45;
System.out.print("n1 value is ="+n1);
System.out.print("n2 value is ="+n2);
System.out.print("n3 value is ="+n3);
n2=n1; // copy value of n1 to n2
n3=n2 // copy value of n3 to n2
System.out.println("----------------------------------");
System.out.print("n1 value is ="+n1);
System.out.print("n2 value is ="+n2);
System.out.print("n3 value is ="+n2);
}
}
more details please follow video:
Comments
Post a Comment