Skip to main content

Java Variable Declarations

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.
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

Comments

Popular posts from this blog

Yahoo! Calendar "Add Event" Seed URL Parameters

I can't seem to find any official documentation on this, so here are my notes. Some information gathered from  http://richmarr.wordpress.com/tag/calendar/ Other information gathered through trial and error, and close examination of the "Add Event" form on Yahoo!'s site. Yahoo! Calendar URL Parameters Parameter Required Example Value Notes v Required 60 Must be  60 . Possibly a version number? TITLE Required Event title Line feeds will appear in the confirmation screen, but will not be saved. May not contain HTML. ST Required 20090514T180000Z Event start time in UTC. Will be converted to the user's time zone. 20090514T180000 Event start time in user's local time 20090514 Event start time for an all day event. DUR value is ignored if this form is used. DUR 0200 Duration of the event. Format is HHMM, zero-padded. MM may range up to 99, and is converted into hours appropriately. HH values over 24 hours appear to be modulated by 24. Durations t...

Java literals:

Java literals:           A constant value which can be assigned to a variable is known as Literal.If we are assigning any outside range value for any data type ,we will get a compile time error saying Possible Loss of Precision found int required byte. For the integral data types (int ,byte,short,long) : we are allowed to specify a literal value in any   one of the following three forms. ---> Decimal literal (normal way) --->Octa literal (prefixed with 0 ) --->Hexa decimal (prefixed with 0x ) int x=10 ------------> Decimal int x=010 ------------>Octa int x=0X10 -------------->Hexa In the Hexa decimal notation for the extra digits we are allowed to specify either small or upper case a,b,c or A,B,C ( this is one of the few places where java is not case sensitive ). Example: class Sample { public static void main(String add[]) { int i = 10; int j=010; int k=0x10; System.out.println( i+”….”+j...