Java Variables
Depends on the content hold by the variables are divided into two categories.
1.reference variable
2.primitive variable
reference variable can be used to hold object references.
Ex: String s=”Laxman”;
Here s is a String object
Primitive variable can be used to hold primitive values.
Example: int i=10;
int[][] a={{1,2},{3,4,5},{6,7,8,9},{}};
for(int i=0;i
System.out.println(a[i].length);
}
output: 2,3 ,4,0
Depends on the position at which it is declared all the variables divided into 3 categories.
1.instance variables/attributes/member variables
2.static variables
3.local variables
Example:
class Student {
String name;
int rollno;
public static void main(String arg[]) {
Student s=new Student();
}
}
Instance variables:
If the values of the variables are varied from instance to instance3, such type of variables are called instance variables.We can declare instance variables with in class ,but outside of any method or block.These are also known as attributes /properties/member variables.The instance variable will create when ever an object is created and destroyed ,whenever garbage collection destroys this object.Instance variable will get default variable no need to perform ,explicit initialization.
Static variables:
A single copy of the static variable will maintain and shared by all instances .the value of the static variable is the same for the instances.The static variable will create whenever the class loaded into the memory and destroy whenever the class is unloaded from the memory.These variables are also known as fields.
Example:
class Student {
String name;
int rollno;
static String collname;
Public static void main(String arg[]) {
Student s1=new Student();
System.out.println(s1.name); // null
System.out.println(collname); // null
}
}
With out s1 and static Compile time error
Static variables will get default values .No need to perform explicit initialization.
System.out.println(Student.collname);
System.out.println(s1.collname);
Static variables we can access by using either class name (highly recommended) or by using object reference.
Local variables:
The variables which are declared inside a method or block or constructor or as method argument are called local variables.Also known as temporary variables /stack variable/automatic variables.The local variables will create as the part of the method execution and will destroy when ever the method terminates.The local variables never get default values and must be initialized before using that local variable.Violation leads to CTE saying variable I might not have been initialized.
Example:
case1:
class Sample{
public static void main(String arg[]){
int i;
System.out.println(”hello”); // hello
} }
Case2: class Sample{
public static void main(String arg[]) {
int i;
System.out.println(i); // CTE variable I might not have been initialized.
} }
Case3: class Sample {
public static void main(String arg[]) {
int i=10;
System.out.println(i); // 10
} }
Case4: class Sample {
public static void main(String arg[]) {
int i;
if(arg.length>0) {
i=20;
}
System.out.println(i); // error
} }
It is not recommended to initialized local variable with in the logical blocks .(But legal)
Case 5:class Sample {
public static void main(String arg[]) {
int i;
if(arg.length>0) {
i=20;
}
else{
i=40;
}
System.out.println(i); // valid
} }
Case6: class Sample{
int[] a;
public static void main(String arg[]) {
Sample s=new Sample();
System.otu.println(s.a); // null
System.out.println(a) // error
System.out.println(s.a[0]); //RTE ---> null pointer exception
System.out.println(a.length); // RTE ---> null pointer exception
} }
case 7: If we declare as static int [] a;
System.out.println(a) // null
System.out.println(a[0]); //RTE ---> null pointer exception
System.out.println(a.length); // RTE ---> null pointer exception
Case8: static int [] a =new int[6];
public static void main(String arg[]) {
Sample s=new Sample();
System.out.println(a) // [I@add234]
System.out.println(a[0]); //0
System.out.println(a.length); //6
}
case9: class Sample {
public static void main(String arg[]) {
int [] a;
System.out.println(a); // error
System.otu.println(a[0]);
System.out.println(a.length);
} }
case10: class Sample {
public static void main(String arg[]) {
int [] a=new int[6];
System.out.println(a); // [I@add34]
System.out.println(a[0]); // 0
System.out.println(a.length); // 6
} }
Once an array object is created its elements will always get default values.
summarization:
Instance array:
int [] a;
System.out.println(objref.a) //null
System.out.println(objref.a[0]); // null pointer exception
System.out.println(objref.a.length); //null pointer Exception
int [] a=new int[6];
System.out.println(objref.a) //[I@add234]
System.out.println(objref.a[0]); // 0 default value for int
System.out.println(objref.a.length); //6
Static array:
static int [] a;
System.out.println(objref.a); //null
System.out.println(objref.a[0]); // null pointer exception
System.out.println(objref.a.length); //null pointer Exception
static int [] a=new int[6];
System.out.println(objref.a) //[I@add234]
System.out.println(objref.a[0]); // 0 default value for int
System.out.println(objref.a.length); //6
Local array:
int [] a;
System.out.println(objref.a) //CTE
System.out.println(objref.a[0]); // CTE
System.out.println(objref.a.length); //CTE
int [] a=new int[6];
System.out.println(objref.a) //[I@add234]
System.out.println(objref.a[0]); // 0 default value for int
System.out.println(objref.a.length); / /6
0 comments:
Post a Comment