Sunday 17 April 2011

Difference between Reference and object



What is difference between reference and object?
http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
References are stored in stack while objects are stored in heap.
Reference holds the memory address of an object, that’s why they are called reference. If they don’t refer to any object in heap, they hold 'null', meaning nothing.

Objects hold the actual data in them. This data is accessed (either read, or written) through reference. An object may be referenced by zero or more references.

For example, consider a class Person, which can hold 'name'.
I create 2 reference variables of Person type.
Person p1;
Person p2;

at this moment both references contain 'null'. An attempt to access 'name' data will throw an error. Now assigning new object to 'p1'.
p1 = new Person();
this causes an object to be created in heap, and its reference(memory address) is stored in p1. Now I can access 'name' data of this object created through p1.

p1.name = "Tom";

Assigning p2 to p1, will make p2 pointing the same object in the heap as p1 is pointing to. Thus if you change 'name' through p2, you are actually changing the same object as p1 is pointing to.

p2 = p1;
p2.name = "Sam";
both p1 and p2 reference variables that are in stack, are pointing to the same object that is in heap.

What is difference between instance and object in Java?


The definition of an instance in an occurrence of something. Since an object in Java is an occurrence of a class, I would say that an object and an instance are one and the same.
In java what is the difference between class variable and instance variable?

The main difference between the class variable and Instance variable is,
first time, when class is loaded in to memory, then only memory is allocated for all class variables. Usually static variables are called class variables. These variables are available throughout the execution of the application and the values are common to the class. You can access them directly without creating an object of the class.

Instance variables are normal variables declared in a class, that would get initialized when you create an instance of the class. Every instance of the class would have a copy of the variable and you need a class instance (object) to access these variables

Difference Between Instance variable and Static Variable?

http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
In java, any variable marked as a static is considered a variable owned by the class. Even if you have a bunch of instances of this class, you only will have one copy of your static variable shared by all the instances.

This is the main difference with instance variables where each instance of your class have his own variables.

Example, if you have a class like this:

public class SoldCar{
public String model; //instance variable - each soldCar have its model
public int cost; //instance variable - each soldCar have its cost
public static int count; //class variable - all soldCars share this count

public SoldCar(){
count ++;
}

public static void main(String[] args){
SoldCar ferrari = new SoldCar();
ferrari.model = "fiorano";
ferrari .cost = 999999;

SoldCar beetle = new SoldCar();
beetle.model = "new beetle";
beetle.cost = 2222;

System.out.println( ferrari.model + "," + ferrari.count );
System.out.println( beetle.model + "," + beetle.count );
}
}

The output of this program is :
fiorano,2
new beetle,2

In oops what is the difference between class variable and variable?
http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
Class Variable is a subset of Variables.

Difference between global variables and local variables?

http://en.site2.answcdn.com/templates/icons/abar_a.gif?v=82319
Global Variable Local Variable 2
It is a variable which is declared outside all the functions
It is a variable which is declared with in a function or with in a compound statement
l It is accessible throughout l It is accessible only within a function/
the program compound statement in which it is
declared

No comments:

Post a Comment