| Author |
Difference between Object reference variable and primitive variable
|
annamalai bala
Greenhorn
Joined: Apr 22, 2009
Posts: 10
|
|
Hi All,
Im new to java can any one please tell me the difference?
for example,
primitive variable,
int i=10;
object reference variable
cat c=new cat();
why can't we just declare it as cat c? what does new cat() means here.
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
Because an object hast state and behavior - unlike a primitive.
And objects need to be created, which you do with the word new. imagine it like a call to a special method which returns a fresh instance of that object.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Vijitha Kumara
Bartender
Joined: Mar 24, 2008
Posts: 3670
|
|
annamalai bala wrote:
Im new to java can any one please tell me the difference?
for example,
primitive variable,
int i=10;
object reference variable
cat c=new cat();
why can't we just declare it as cat c? what does new cat() means here.
"new Cat()" is what creates the actual object on the heap. "Cat c" is where you declare the reference to that object. That statement creates Cat object in the heap and assigns it to the "c" reference which you can use to manipulate the object (reference is like a remote control to a TV which you can use to control it). When it comes to primitive variables they are the "value holders".
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Joydeep Ghatak
Ranch Hand
Joined: Sep 30, 2009
Posts: 41
|
|
Well,variables comes in two flavors, primitive and Object reference.
Primitive variables contains fundamental values, like int, float, boolean etc, where "Object reference" contains/refers the handle of the Object that has been created using the "new <ObjectName()>" syntex.
Here int i = 10; means ==> 'i' is the name of the primitive variable, whose data type is integer (means it can hold integer type values) and currently it is holding its value as 10.
and cat c=new cat(); this line can be written in another way as,
Cat c ;
c = new Cat();
Where c is the reference of the 'object', that has been created using "new Cat()", and where Cat() is the user defined class.
With 'new Cat()', you are creating object and allocating memory to the class Cat() and after creating the object, you can refer/access the "Cat object", with the reference/handle 'c'.
|
“Men are only as good as their technical development allows them to be.”
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
Nice explanation.
I have changed the text colour to black; that is easier to read.
|
 |
annamalai bala
Greenhorn
Joined: Apr 22, 2009
Posts: 10
|
|
|
Thanks a lot everyone
|
 |
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
|
|
I just realized something myself while reading through this thread. The separation of the reference variable declaration
Cat c;
and the instance creation and assignment
c = new Cat();
makes more sense when you consider being able to do something like
Animal c = new Cat();
or
Animal d = new Dog();
|
 |
 |
|
|
subject: Difference between Object reference variable and primitive variable
|
|
|