Variable:
The name of memory allocation which is used to store the data type during time execution.it is a combination of two words “vary or able” which means its value can be changed.
Deceleration:
We declare the variable as: Data_type Varible_Name;
Initialization:
We initialize the variable as: Varible_Name=value;
Definition:
We define the variable like as: Data_type Varible_Name = Value; Example:
Int y; (Declaration of variable)
y=10; (Initialization of variable)
Int x=10; (Definition of variable)
Types of Variables
There are three types of variable are as follows:
1. Local variable:
2. Instance variable:
A type of variable that is declared inside the class and outside the function. It accesses the value through the object.
3. Static variable:
A type of variable that is
declared with a static keyword throughout the program.
Example to understand the types of variables in java
Program:
class Variable {
static int x=23; //static variable
float instance=99; //instance variable
void local(){ //local variable
double a=34;
float b=45.5f;
double ab=a+b;
System.out.println("Sum of ab is: "+ab); System.out.println("The value of X: "+x);
}
public static void main(String[] args) {
Variable obj=new Variable();
obj.local();
System.out.println("Instance value: "+obj.instance); }
}
Output:
Sum of ab is: 79.5
The value of X: 23
Instance value: 99.0
★ How do we create the object in java?
We create the object as:
Class_name object_Name = new Class_function();
Here we discuss some examples of variables in java.
★Add Two Numbers
class Add {
public static void main(String[] args) {
float a=12.5f;
double b=44.6d;
System.out.println("The sum of two numbers is: "+a+b);
}
}
Output:
The sum of two numbers is: 12.544.6
★Widening:
Widening is also known as upcasting. It is the conversion of greater primitive type value into smaller primitive type value.
Example to understand the widening in java
Program:
class Widening {
public static void main(String[] args) {
int x=23;
float a=x;
long b=x;
double c=x;
System.out.println("Float: "+a);
System.out.println("Long: "+b);
System.out.println("Double: "+c);
}
}
Output:
Float: 23.0
Long: 23
Double: 23.0
★Narrowing(Typecasting):
Narrowing is also known as downcasting. The conversion of narrowing is small primitive data type value into large primitive data type value.
Example to understand the narrowing in java
Program:
class Narrowing {
public static void main(String[] args) {
double a=23.5;
float d=55.05f;
long b=(long)a;
double c=(double)a;
int x=(int)a;
float y=(float)a;
byte z=(byte)d;
System.out.println("Long: "+b); System.out.println("Double: "+c); System.out.println("Int: "+x);
System.out.println("Float: "+y); System.out.println("Byte: "+z);
}
}
Output:
Long: 23
Double: 23.5
Int: 23
Float: 23.5
Byte: 55
★ Overflow
Program:
class Overflow {
public static void main(String[ ] args) { double db=150d;
byte byt=(byte)db;
System.out.println("byte"+byt);
}
}
Output:
byte-106
★ Underflow
Program:
class Underflow {
public static void main(String[ ] args) { double db=-200d;
byte byt=(byte)db;
System.out.println("Byte "+byt);
}
}
Output:
Byte 56
Comments
Post a Comment