• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Difference between Object reference variable and primitive variable

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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".
 
Ranch Hand
Posts: 43
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'.
 
Marshal
Posts: 76845
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation.

I have changed the text colour to black; that is easier to read.
 
annamalai bala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everyone
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic