Variables
Variables acts as a container for the user data. It is a named area that is allocated by the user and space will be allocated according to its datatype. Variable needs to be declared before using them.Value of variable can be changed any number of times during the execution of program.
For example:
int a; //declaring an integer type variable.
a = 5; //assigning value to a variable.
- Local Variables : Variables that are declared inside a members are called Local Variables. Local variables are necessary to initialized.Their value is varied from object to object.
* Only Final modifier is used with local keywords.
* They can be initialized in static and instance block.
* Stored inside stack memory.
* Scope of these variables is the block in which we declared them.
* Created when block in which declared is executed.
* Destroyed when block in which declared is completed.
* JVM doesn't provide any default value, thus it is mandatory to initialized them for accessing.
* Local variables are thread safe as for every thread a separate copy is created.
{
int a; // Instance variable.
static int b; //Static variable.
public static void main(String arg[])
{
System.out.println(a);
int c; //Local variable.
c = 5;
System.out.println(c);
}
}
Ways to deal with Variables -
* Stored inside stack memory.
* Scope of these variables is the block in which we declared them.
* Created when block in which declared is executed.
* Destroyed when block in which declared is completed.
* JVM doesn't provide any default value, thus it is mandatory to initialized them for accessing.
* Local variables are thread safe as for every thread a separate copy is created.
- Instance Variables : Variables that are defined directly inside a class without static keyword are known as Instance variables.These variables can have difference values across different objects.
* A separate copy of variable will be created for every object
* Created at the time of object creation and destroyed at the time of object distraction.
* Stored in Heap memory as a part of object.
* Can.t access directly from static context, but we can access them by using object reference.
* JVM provides default values for instance variables.
* Instance variables are not thread safe because multi-thread can be accessed simultaneously
- Static Variables : Variables that defined directly inside a class with a Static keyword are known as Static variables(also known as Class variables).These variables can only have one value.
* A single copy will be created and shared by every object of class.
* Created at the time of class loading and destroyed at the time of class unloading.
* Stored in memory ares.
* Scope is same as of .class file
* Can be access either with object reference or class name directly.
* Can access directly from instance as well as static context.
* JVM provide default value for static variables.
* Static variable are also not thread safe as the can access multi-thread simultaneously.
For Example :
class abc{
int a; // Instance variable.
static int b; //Static variable.
public static void main(String arg[])
{
System.out.println(a);
int c; //Local variable.
c = 5;
System.out.println(c);
}
}
Ways to deal with Variables -
- Declaring a variable in one statement:
Syntax
[modifiers] <Datatype> <Variable>;
Example
int i;
byte b;
char c;
double d;
float f;
String s;
Hello h; etc.
- Initializing single variable:
Syntax
<Variable_Name> = <value / expression>;
Example
i = 5;
c = 'A';
b = 4 + 5;
d = 59.64;
[modifier] <Datatype> <Variable_Name> = <value/expression>;
Example
int a = 9;
float f = 45.11;
int x = 44 + 16;
boolean b = true;
<Datatype> <variable1>, <variable2> , <variable3>.... ;
Example
int a,b,c;
float p,q,r,s;
String s1,s2,s3;
<variable1> = <variable2> = <variable3>... = <value/expression>;
Example
a = b = c = 25;
p = q = r = s = 66.23;
str1 = str2 = str3 = "Hello";
[modifier] <Datatype> <variable1> = <value1>, <variable2> = <value2>, <variable3> = <value3>.... ;
Example
int a = 1, b = 4, c = 6, d = 59;
String str1 = "Welcome", str2 = "To", str3 = "My_Blog";
boolean x = true, y = false;
Key Points To Remember About Variables:
c = 'A';
b = 4 + 5;
d = 59.64;
- Declaring and Initializing a variable in a single statement:
[modifier] <Datatype> <Variable_Name> = <value/expression>;
Example
int a = 9;
float f = 45.11;
int x = 44 + 16;
boolean b = true;
- Declaring multiple variables in a single statement:
<Datatype> <variable1>, <variable2> , <variable3>.... ;
Example
int a,b,c;
float p,q,r,s;
String s1,s2,s3;
- Initialize multiple variables with same value:
<variable1> = <variable2> = <variable3>... = <value/expression>;
Example
a = b = c = 25;
p = q = r = s = 66.23;
str1 = str2 = str3 = "Hello";
- Declaring and Initializing multiple variables in single statement:
[modifier] <Datatype> <variable1> = <value1>, <variable2> = <value2>, <variable3> = <value3>.... ;
Example
int a = 1, b = 4, c = 6, d = 59;
String str1 = "Welcome", str2 = "To", str3 = "My_Blog";
boolean x = true, y = false;
Key Points To Remember About Variables:
- Local variables value must be provided by user.
- Static and Instance variables are initialized by JVM.
- Static variables can be directly accessed from static content.
- Default value for static variable is 0.
- Same name variables can also be declared but in different scope of same class.
Wow,what a skimmed content about the Variables.
ReplyDeleteCould you recommend us a genuine book for further reading?