JavaIntellectualTricks

Variables

Types of variable:
Based on type of value represented by a variable, all variables are divide in to 2 types.
1.Primitive variables
Primitive variables can be used to represent primitive values.
Example:
int x=10; // here x is called as primitive variables
2.Reference variables
Reference variables can be used to refer objects
Example:
Student s=new Student(); // here s is called as reference variable

Based on position of declaration and behavior all variables are divide in to 3 types.
1. instance variables
2. static variables
3. local variables

Instance variables:
  • If the value of a variable is varied from object to object, such types of variable are called as instance variables.
  • For every object, a separate copy of instance variable is created.
  • Instance variables should be declared with in the class directly, but outside of any method or constructor or block.
  • Instance variables are created when an object is created and destroyed at the time of object destruction, hence the scope instance variable is exactly same as object.
  • Instance variables will be stored in the heap memory as a part of object.
  • We can't access instance variables directly from static area, but we can access by using object reference.But we can access instance variables directly from instance area.
Example:
class Test{
int x=100;
public static void main(String [] args){
System.out.prinln(x); // C.E Non-static variable x cannot be referenced from a static context
Test t= new Test();
System.out.prinln(t.x);  //100
}
public void m1(){
System.out.prinln(x);  //100
}
}
For instance variables JVM is always providing default initial value and we are not required to perform initialization explicitly.
Example:
class Test{
int i;
boolean b;
double d;
String s;

public static void main(String [] args){
Test t= new Test();
System.out.prinln(t.i);  //0
System.out.prinln(t.b); //false
System.out.prinln(t.d); //0.0
System.out.prinln(t.s); //null
}
}
Instance variables are also known as object level variables or attributes.

Static variables:
  • If the value of the variable not varied from object to object, then it is not recommended to declare variable as instance variable. We have to declare such type variables as class level by using static modifier.
  • In the case of instance variables for every object a separate copy will be created, but in the case of static variables, a single copy will be created at class level and shared by every object of the class.
  • Instance variables should be declared with in the class directly, but outside of any method or constructer or block.
  • Instance variables are created at the time of class loading and destructed at time of class unloading, hence the scope of the static variables is exactly same as the scope of the .class file.

If we are trying to run a simple java program the following steps are carring on:-
> java Test

1. start the JVM.
2. create and start main thread.
3. Locate the Test.class file
4. Load the Test.class file
5. Execute main() method
6. Unload  Test.class file
7. Terminate main thread
8. Shutdown the JVM

static variables will be stored in method area.
We can access static variable either by object reference or by class name, but recommended to use class name.
With in the same class it is not required to use class name and we can use directly.
class Test{
int x=100;
public static void main(String [] args){
System.out.prinln(x); // C.E Non-static variable x cannot be referenced from a static context
Test t= new Test();
System.out.prinln(t.x);  //100
}
public void m1(){
System.out.prinln(x);  //100
}
}

For instance variables JVM is always providing default initial value and we are not required to perform initialization explicitly.
Example:
class Test{
int x=10;
public static void main(String [] args){
Test t= new Test();
System.out.prinln(t.x);  //10
System.out.prinln(Test.x); //10
System.out.prinln(x); //10
}
}
We can access static variables directly from both instance and static areas.
class Test{
int x=100;
public static void main(String [] args){
System.out.prinln(x); // C.E Non-static variable x cannot be referenced from a static context
Test t= new Test();
System.out.prinln(t.x);  //100
}
public void m1(){
System.out.prinln(x);  //100
}
}

For instance variables JVM is always providing default initial value and we are not required to perform initialization explicitly.
Example:
class Test{
int i=10;
public static void main(String [] args){
System.out.prinln(i);  //10
}
public void m1(){
System.out.prinln(i);  //10
}
}
For static variables JVM will provide default values and we are not required to perform initialization explicitly.
Example:
class Test{
static int i;
static boolean b;
static double d;
static String s;

public static void main(String [] args){
System.out.prinln(Test .i);  //0
System.out.prinln(Test .b); //false
System.out.prinln(Test .d); //0.0
System.out.prinln(Test .s); //null
System.out.prinln(i);  //0
System.out.prinln(b); //false
System.out.prinln(d); //0.0
System.out.prinln(s); //null
}
}
static variables are also known as class level variables or fields.
Example:
class Test{
static int x=10;
int x=20;
public static void main(String [] args){
Test t1=new Test();
t1.x=111;
t1.y=222;
Test t2=new Test();
System.out.prinln(t2.x+”--”+t2.y);  //111--20
}
}

Local variables:
  • The variables which are inside the constructor, block or method are called local variables.
  • Sometimes to meet temporary requirements of the programmer, we can declare a variable inside a method, block or constructor . Such type of variables are also called as local/automatic/stack/temporary variable.
  • Local variables will be stored inside stack memory.
  • Local variables will be created while executing the block in which we declared it .
  • Once block execution completes, automatically local variables will be destroyed. Hence the scope of local variable is the block in which we declared  it.

Example 1:
class Test{
public static void main(String [] args){
int i=0;
for(int j=0;j<3;j++){
i=i+j;
}
System.out.prinln(i+” ”+j);  // C.E cannot find symbol, symbol: variable j, location: class Test
}
}
Example 2:
class Test{
public static void main(String [] args){
try{
int j=Integer.parseInt(“ten”);
}
catch(NumberFormatException  nfe){
j=10;  // C.E cannot find symbol, symbol: variable j, location: class Test
}
 System.out.prinln(j);  // C.E cannot find symbol, symbol: variable j, location: class Test
}
}

For local variables JVM won't provide default values, compulsorily we should perform initialization explicitly before using that variable i.e if we are not using then, it is not required to perform initialization.

Example 1:
class Test{
public static void main(String [] args){
int i;
System.out.prinln(“Hello”); // Hello
}
}
Example 2:
class Test{
public static void main(String [] args){
int i;
System.out.prinln(i); // C.E : variable i might not have been initialized
}
}
Example 3:
class Test{
public static void main(String [] args){
int i;
if(args.length>0){
x=10;
}
System.out.prinln(i); // C.E : variable i might not have been initialized
}
}
Example 4:
class Test{
public static void main(String [] args){
int i;
if(args.length>0){
x=10;
}
else{
x=20;
}
System.out.prinln(i);  // No Error //Java Test  A B then o/p=10, //Java Test  then o/p=20
}
}
Note :
1. It is not recommended to perform initialization for local variables inside logical blocks because there is no guarantee for the execution of these blocks always at run time.
2. It is highly recommended to perform  initialization for local variables  at the time of declaration at least with default values.
3. The only applicable modifier for local variables is final. Bymistake if we are trying apply any other modifier then we 'll get compile time error.

Example:
class Test{
public static void main(String [] args){
public int x=10; // C.E Illegal start of expression
private int x=10; // C.E Illegal start of expression
protected int x=10; // C.E Illegal start of expression
static int x=10; // C.E Illegal start of expression
transient int x=10; // C.E Illegal start of expression
volatile int x=10; // C.E Illegal start of expression
final int x=10; // No error
}
}
Note:
If we are not declaring with any modifier then by default it is default. But this rule is applicable only for instance and static variables but not for local variables.

Conclusions:
1. For instance and static variables JVM is providing deafult values and we are not required to perform initialization explicitly. But for local variaable JVM won't provide deafult values and we are  required to perform initialization explicitly before using that variable.
2. Instance and static can be accessed by multiple threads simultaneously and hence these are not thread safe. But in the case of local variables for every thread a separate copy will be created and hence local variables are thread safe.
3. Every variable in java should be either instance or static or local. Every variable in java should be either primitive or reference. So the various possible combinations of variable in java are instance-primitive, instance-reference, static-primitive, static-reference, local-primitive and local-reference.

UnInitialized arrays:
class Test{
int[] x;
public static void main(String [] args){
Test t=new Test();
System.out.println(t.x); //null
System.out.println(t.x[0]); //NullPointerException
}
}

Instance level:
int[] x;
System.out.println(obj.x); //null
System.out.println(obj.x[0]); //NullPointerException

int[] x=new int[3];
System.out.println(obj .x); // [I e3563@5
System.out.println(obj .x[0]); //NullPointerException

Static level:
static int[] x;
System.out.println(obj.x); //null
System.out.println(obj.x[0]); //NullPointerException


static int[] x=new int[3];
System.out.println(obj .x); // [I e3563@5
System.out.println(obj .x[0]); //NullPointerException

Local level:
int[] x;
System.out.println(obj.x); // variable x might not have been initialized
System.out.println(obj.x[0]); // variable x might not have been initialized

int[] x=new int[3];
System.out.println(obj .x); // [I e3563@5
System.out.println(obj .x[0]); //0

No comments:

Post a Comment