Depending on the Content
Holds by variable they are divided into TWO categories
1.
Primitives
Variable: A primitive can be one of eight types: char, boolean,
byte,short, int, long, double, or float. Once a primitive has been declared,
its primitive type can never change, although in most cases its value can
change.
2.
Reference variables A reference
variable is used to refer to (or access) an object. A reference variable is
declared to be of a specific type and that type can never be changed.
Reference variable can be used to hold object references.
Ex: String string1=”Sekhar4J”;
Here string1 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 <= a ;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/class variables
3.local variables
Example:
class Employee {
String name;
int employeeNumber;
public static void main(String arg[]) {
Employee employeeObj=new Employee Employee ();
}
}
Instance variables:
·
If the values
of the variable are varied from instance to instance, such type of variable is
called instance variable.
·
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 whenever 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 Employee {
String empName;
int empNumber;
static String companyName;
Public static void main(String arg[]) {
Employee e1=new Employee ();
System.out.println(e1.empName); // null
System.out.println(companyName); // null
}
}
With out e1 and static Compile time error
Static variables will get default values .No need to perform explicit initialization.
System.out.println(Employee.companyName);
System.out.println(e1.companyName);
Static variables we can access by using either class name (highly recommended) or by using object reference.
Example:
class Employee {
String empName;
int empNumber;
static String companyName;
Public static void main(String arg[]) {
Employee e1=new Employee ();
System.out.println(e1.empName); // null
System.out.println(companyName); // null
}
}
With out e1 and static Compile time error
Static variables will get default values .No need to perform explicit initialization.
System.out.println(Employee.companyName);
System.out.println(e1.companyName);
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 whenever
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
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
Comments
Post a Comment