• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

arrays -- obj vs ref var to obj

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S-B, pg 29:

Car [] myCars = {new Subaru(), new Honda(), new Ferrari()};

It helps to remember that the elements in a Car array are nothing more than Car reference variables.


"reference variables"? even if using 'new' keyword to create vars contained in array?

this whole obj/ref to obj thing can be confusing...

take this for example, from pg 24:

int x = 9;
int[] dots = {3,6,x,8};

Line 2 in the preceding code does four things:
-- Declares an int array reference variable named dots.
-- Creates an int array with a length of four (four elements).
-- Populates the elements with the values 3, 6, 9, and 8.
-- Assigns the new array object to the reference variable dots.


so, 2nd line here: creates an int array ref var named 'dots'... AND assigns the new Array obj to the ref var 'dots'.. is this the same thing as saying, "creates an array obj named 'dots'"... ???

also would like to know, if creating arrays in this 'shortcut' fashion (with curly braces), since you're not using 'new' keyword, does it also create an Array obj (as opposed to ref var..) thank you...

-v
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veronica,


"reference variables"? even if using 'new' keyword to create vars contained in array?




The author refers to elements in array. So imagine that each array element is a separated variable existing in memory. As all array elements refer to objects, so for sure they are reference variables.



so, 2nd line here: creates an int array ref var named 'dots'... AND assigns the new Array obj to the ref var 'dots'.. is this the same thing as saying, "creates an array obj named 'dots'"... ???
[/QUOTE/


To be more clear, I prefer to say : "Creates a reference variable for an object named 'dots'"



also would like to know, if creating arrays in this 'shortcut' fashion (with curly braces), since you're not using 'new' keyword, does it also create an Array obj (as opposed to ref var..) thank you...




I think you're confusing the terms Reference Variable and Object, so let me try to clarify it.

Object : As you read in K&B's book, any array is an object by itself. What it means ? It means that when you create an array object using either new keyword or curly braces, you are simply saying to JVM : "Please, could you allocate some more memory and put my object there ?". That's all. No more things happen.

Reference Variable : This is the second stage in an assignment operation.

After you ask JVM to create the object, now you say : "Please, give a way to access this new object using a suggestive name."

At this point, the JVM RECORDS the object memory address to your reference variable, so that now you have a way to access your object contents.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's something more to say and there are some corrections to made to "Bessa"'s reply.


"Please, could you allocate some more memory and put my object there ?"



Actually this is not 100% correct. When creating an array of objects, it doesnt requst to allocate some more memory and put the objects into it. What it does is, it request the JVM to allocate an array which we can hold references to the given type of objects. Array just hold the reference of the object, and the object exists in a seperate location..


int x = 9;
int[] dots = {3,6,x,8};



x is not an reference. as long as x is a primitive variable, x is copying it's value to the array and it doesnt called a reference variable.
[ February 13, 2006: Message edited by: Roshan Amadoru ]
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with both Bessa and Amadoru.

Consider these two scenarios

Car [] myCars1 = {new Subaru(), new Honda(), new Ferrari()}; // 1

Car [] myCars2=new Car[3]; // 2
myCars2[0]=new Subaru();
myCars2[1]=new Honda();
myCars2[2]=new Ferrari();

"Please, could you allocate some more memory and put my object there ?".



The above statement is with respect to statement 1

Actually this is not 100% correct. When creating an array of objects, it doesnt requst to allocate some more memory and put the objects into it. What it does is, it request the JVM to allocate an array which we can hold references to the given type of objects. Array just hold the reference of the object, and the object exists in a seperate location..



The above statement is with respect to statement 2.

As we are declaring and creating the array object reference myCars1, it is appropriate to consider statement1.

As we are just declaring array object reference myCars2 but not really creating objects to hold, it is appropriate to consider statement2.

Correct me if I'm wrong guyz

Regards,
Narendranath
[ February 12, 2006: Message edited by: Naren Chivukula ]
 
Roshan Amadoru
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naren,

That's not true. Because, even in 1st case, it allocate memory for the objects you create in a seperate location and get the address (reference) of the created object and then assign that reference value to the particular array element. It's just like this.


here what happen is "new Object()" creates a new object and returns the reference of the newly creted instance and assign that reference into the object reference variable o. so here, o is a reference variable and what "new object()" returns is a object reference.
[ February 13, 2006: Message edited by: Roshan Amadoru ]
 
Naren Chivukula
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roshan, What you said I agree. But what Bessa was trying to tell, I depicted. In the first statement,
1. JVM creates 3 car objects on to the heap
2. Creates array object on to the heap containing the addresses (references) of the above 3
3. Gives a handle i,e.. an object reference for the array object created.

So the above three steps will take place for a single statement 1. So it is as same as creating an object with new operator. like.
Object o=new Object(); //3

So statement 1 and 3 both causes JVM to actually allocate the memory space.

Regards,
Narendranath
 
Veronica Damian
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much for yr responses... have been so busy have not had time to look @ responses yet (will do so over the weekend), but wanted to thank respondents..


-v
 
No more fooling around. Read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic