Types of Variable and Type Casting
There are three types of variables in java
|
Local Variable
| A variable that is declared inside the method is called local variable. |
Instance Variable
| A variable that is declared inside the class but outside the method is called instance variable . |
Static variable
| A variable that is declared as static is called static variable. It cannot be local. Type Casting Assigning a value of one type to a variable of another type is known as Type Casting. Type Casting is classified into two category :
Examples of implicit type casting
Class ImplicitTypeCasting{
public static void main(String args[])
{
int i=100;
long l =i; //implicit type conversion
float f=l; //implicit type conversion
System.out.println("Int value: "+i);
System.out.println("Long value: "+l);
System.out.println("Float value: "+f);
}
}
output:
Int value: 100
Long value: 100
float value: 100.0
Example of explicit type casting
Class ExplicitTypeCasting{
public static void main(String args[])
{
double d=100.04;
long l =(long)d; //explicit type conversion
int i =(int)l; //explicit type conversion
System.out.println("Double value: "+d);
System.out.println("Long value: "+l);
System.out.println("Int value: "+i);
}
}
output:
Double value: 100.04
Long value: 100
Int value: 100
|
No comments:
Post a Comment