![]() |
Variable Declaration in java |
Java Variables Store Data Value
Java 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.
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 “very + able” combination, which suggests that its worth is often modified.
data_type variable_name=value; int data=100; float=4.7; char gender='m'; |
Rules of Variables in Java
1. The First letter of a variable should be
an alphabet or underscore like this a(_).
2. The First letter of a variable should not
be a digit.
3. variable name should not be a reserved
keyword.
Types of Variables in Java
There square measure 3 styles of variables in java.
1. local variable
2. 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 the victimization of the static keyword.
2. Instance variable: A
variable declared within a category outside the body of a technique is
termed the Associate in Nursing instance variable. it's not declared static. It is
referred to as the Associate in Nursing instance variable as a result it's worth
being instance specific and isn't shared between instances.
3. Static variable: A variable declared static is termed a static variable. It cannot be native. you'll produce a duplicate of a static variable and use it for all category instances. Allocating memory for a static variable happens one time, once the category is loaded into memory.
How To Use Local Variable in Java?
class codeswihbaby { 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) { codeswithbaby obj=new codeswithbaby(); obj.add(); } }
*****OUTPUT***** Addition of a+b=300 |
How To Use Instance Variable in Java?
class codeswithbaby { 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) { codeswithbaby obj=new codeswithbaby(); obj.add(); obj.sub(); } }
*****OUTPUT***** Addition of a+b=300 Subtraction of a-b=100 |
How To Use static Variable in Java?
class codeswithbaby { static String name="Welcome To codeswithbaby website"; public static void main(String[] args) { System.out.print(icoder.name); } } *****OUTPUT***** Welcome To the codeswithbaby website |
How To Use Widening Variable in Java?
public class Simple{ public static void main(String[] args){ int a=10; float f=a; System.out.println(a); System.out.println(f); }}
*****OUTPUT***** 10 10.0 |
How To Use Typecasting Variable in Java?
public class Simple{ public static void main(String[] args){ float f=10.5f; //int a=f;//Compile time error int a=(int)f; System.out.println(f); System.out.println(a); }}
*****OUTPUT***** 10.5 10 |