Friday, September 23, 2011

3.JAVA BASICS.. CLASS AND OBJECTS WITH APPROACH OF MEMORY

What is a class?
We can say class is a template of an object. Objects exists in memory. Class is just a way to define a structure of object.

Now we start with discussion of memory.
Four segments of memory
1)Data segment
2)Code segment
3)Stack segment
4)Heap segment

In C we have seen the use of code and data segment. But in java we will be dealing with Stack Segment and Heap Segment.

See the example below

When we write using new keyword, a new object is created and new Customer() returns the hashcode of the created object. The variable used to store the hash code value is called reference variable.It is called reference because it has the hash code value,which refers to an object.

In short we can say that obj(the variable used above) stores the hash code,where hash code is not the actual address(it is changed to hash code by some hash algorithm). Java does not deal with direct memory,it deals with hash code and the hash code  indirectly refers to a memory. This is the reason why java is called secure. Unlike C,it has no pointers or depointers which can take you to the actual memory address.

Suggestions: 
When ever dealing with output problems,always make a heap by making a square and see the code for how many objects are there. Count and make that many ovals inside the square and point these objects by an arrow of reference. I assure you by using this method,you will not make any mistakes. Example is below

Now an important note,to be always kept in mind and will solve all your problems.
         Local Variables   ------->  STACK
         Global Variables  ------->   HEAP
Keep the above note always in your mind and there will never be a chance of mistake.Here global refers to be a part of class definition.
Stack Variables do not have any default value
Heap variables have default value. If there is any variable in Stack and you try to use that variable without initialization then it will result in a compile time error.
All the objects which are created using new keyword,goes to heap memory,while the location of these references depends,whether they are local or global.
    Take a simple example::
  Sample sample=new Sample();
Here the object of class Sample will always be in heap memory,while the reference(sample here)  location depends on whether it is declared locally or global. We can say that if sample is inside a method then it will be in stack,and if it is inside a class(but outside methods) then it will be stored in heap memory.

Default values of all the variables(only Heap Variables)
int       ---> 0
byte     --->0
short    --->0
boolean -->false
long    ---->0
float     ---->0.0f
double  ---->0.0
char     ----->'\0'
Reference Variable   ---> NULL

NOTE: From above,keep in mind,all variables  keep 0 as the default value.. false(boolean) can be related with 0,NULL can be related with 0,'\0' can be related with 0. Keep in mind a default value of 0.

By seeing any class you can relate the variables with stack and heap.

We will do some examples and then proceed
Class Sample{
int a;                //   Global Variable =>heap              
void printVariable()
{
int b;               //     Local Variable => Stack                 
System.out.println(a);
System.out.println(b);                         ///Error
}
}

Why Error?
The reason is that b is a local variable. We are trying to use a local variable,which is not initialized,and stack variable don't have  default value so it gives error. 
Comparison with C language: In C also,global variables prints 0 while the local variable gives Garbage Value.
Java does not want to have these garbage values because that will make your code buggy,so Java compiler is checking for this at compile time only and thus ,doesn't allow the code to compile.

Example 2:
Class Sample{
Test obj;            //   suppose Test is another class
int a;
void printVariables()
{
System.out.println("value of a is:"+a);
System.out.println("value of reference:"+obj);
}
}
Ans: As we can see that both are global variables,means both use heap memory.
None of them is initialized,so default value is printed.
    value of a is: 0
    value of reference: NULL

Example 3:
class Sample{
void printArea()
{
 int a;
int b=1;
if(b==1)
{
a=10;
}
System.out.println(a);
}
Ans: Compile Time Error
Now the question that come in your mind, the local variable is initialized before using,then why it gives compile time error.
Reason: Even though you have initialized the variable,but at compile time, the compiler does not know in advance if it will enter the loop or not at run time. If suppose,at run time it does not enter the loop and the variable will not get initialized. To avoid these type of situations at run time,java is designed such a way,if a variable is not initialized and gets initialized in inner loops,it does not matter. So if any variable is initialized in inner loop but not in the loop where it is declared,and we use the variable in the declaration loop,the code will not compile.

1 comment: