![]() |
java Variables How To Store Data Values |
Java Variables How To Store Data Value
Variables: A variable could be an instrumentality that stores a worth once running a java program.
The variable receives an information kind. The variable is the name of a memory location.
There square measure three styles of variables in java native, instance, and static.
There square measure two styles of knowledge in java: primitive and non-primitive.
data_type variable_name=value;
int data=100;
float=4.7;
char gender='m';
Variable: The variable is the name of the reserved space allotted in memory.
In alternative words, it's the name of the memory location.
it's a “vary + able” combination, which suggests that its worth are often modified.
Important Variable Topic in Other Languages
👉C++ Variables
👉C Variables
👉Python Variables
Rules of Variables in Java
2. The First letter of a variable should not be a digit.
Types of Variables in Java
There square measure 3 styles of variables in java.
1.local variable2.instance variable
3.static variable
Local variable: A variable declared within the body of a technique is termed a neighborhood variable.
you'll solely use this variable during this methodology, and also the alternative ways within the category do not even apprehend that this variable exists.
A local variable can not be set to victimization the static keyword.
2. Instance variable: A variable declared within a category however outside the body of a technique is termed Associate in Nursing instance variable. it's not declared static.
It is referred to as Associate in Nursing instance variable as a result of its worth is instance specific and isn't shared between instances.
How To Use Local Variable in Java?
class icoderweb
{
int a=200;
int b=100; //local variable declared
int c;
public void add()
{
c=a+b;
System.out.print("Addition of a+b="+c);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.add();
}
}
*****OUTPUT*****
Addition of a+b=300
How To Use Instance Variable in Java?
class icoderweb
{
int a=200;
int b=100;
int c;
public void add()
{
c=a+b;
System.out.print("Addition of a+b="+c);
}
public void sub()
{
c=a-b;
System.out.print("Subtraction of a-b="+c);
}
public static void main(String[] args)
{
icoderweb obj=new icoderweb();
obj.add();
obj.sub();
}
}
*****OUTPUT*****
Addition of a+b=300
Subtraction of a-b=100
How To Use static Variable in Java?
class icoderweb
{
static String name="Welcome To Icoderweb website";
public static void main(String[] args)
{
System.out.print(icoder.name);
}
}
*****OUTPUT*****
Welcome To the Icoderweb website