What is Variable in JAVA ?
What is Variable in JAVA ?
- It is a name of storage space which is used to store data.
- It's value may be changed.
- It always contains last value stored to it.
- It is always declared with data type.
Variables store data for processing.
A variable is given a name (or identifier), such as area, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored.
Variables have types. Some examples:
- int: for integers (whole numbers) such as 123 and -456
- double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -55.66.
- String: for texts such as "Hello" or "Good Morning!". Text strings are enclosed within double quotes.
You can declare a variable of a type and assign it a value.
Example:
String name = "David";
JAVA
It is important to note that a variable is associated with a type, and is only capable of storing values of that particular type. For example, an int variable can store integer values, such as 123; but it cannot store real numbers, such as 12.34, or texts, such as "Hello"
Variable Declaration
int rollno; float marks; char grade;
- Here rollno is a variable of type int ,marks is a variable of type float and grade is a variable of type char.
Variable Initialization
- We can initialize a variable at the time of declaration of variable or after declaration.
int rollno=201; float marks=85.6; char grade='A';
- Here 201 is the value of rollno,85.6 is the value of marks and A is the value of grade. Character value is always written in single quotes.
Other method of variable Initialization
- We can also initialize a variable after the time of declaration of variable.
int rollno; float marks; char grade; rollno=201; marks=85.6; grade='A';
Rules to define a variable
- The first letter of a variable should be alphabet or underscore(_).
- The first letter of variable should not be digit.
- After first character it may be combination of alphabets and digits.
- Blank spaces are not allowed in variable name.
- Variable name should not be a keyword.
Types of Variable
- Local Variable
- Instance Variable
- Static Variable
Local Variable
- A variable declared inside the body of the method or constructor or block is called local variable.
- Local variable can be used only inside that method/function in which it is declared.
- A local variable can be a static variable.
class Easy
{
public void add()
{
int x,y=10,z=20;//declaring local variable
x=y+z;
System.out.println("Add="+x);
}
public static void main(String[] args)
{
Easy obj=new Easy();
obj.add();
}
}
/*
### Output ###
Add=30
*/
Instance Variable
- A variable which is declared inside a class but outside the body of the method or constructor or block is called instance variable.
- Instance variable can be used anywhere in the program.
class Easy
{
//these instance variabe can be used
//inside all the function of this class
int x,y=30,z=20;
public void add()
{
x=y+z;//accessing instance variable
System.out.println("Add="+x);
}
public void sub()
{
x=y-z;//accessing instance variable
System.out.println("Sub="+x);
}
public static void main(String[] args)
{
Easy obj=new Easy();
obj.add();
obj.sub();
}
}
/*
### Output ###
Add=30
Sub=10
*/
Static Variable
- A variable which is declared with static keyword, inside a class but outside the body of the method or constructor or block is called static variable.
- Static variable is stored in the static memory.
- Static variables are created when the program starts and destroyed when the program stops.
- Static variable can be called by class name directly.
class Easy
{
//declaring static variable
static String text="You are a smart guy.";
public static void main(String[] args)
{
System.out.println(Easy.text);
}
}
/*
### Output ###
You are a smart guy.
*/
Comments
Post a Comment