wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array Objects" Watch "Array Objects" New topic
Author

Array Objects

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Sample s = new Sample();
Sample [] s = new Sample[4];

In first case, reference s is created on stack and object Sample is created on heap.
In second case, how does it work ? is it like that s1, s2, s3 , s4 are created on stack and objects they refer to are created on heap ?
Then where is the array object created, as array itself is an object. So in total, there are five objects created. Where are the five references and how are they created ?
I guess I am confused.
Pls reply
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Hi First of all..in the following case :

Sample[] s = new Sample[4];

Only one Object will be created on the heap, An array Object...and
this Object can actually can hold the reference to Other Object..so In very simple language..
this array Object is like a stack( But actually it is not..),this array object resides on heap..and has fixed size of 4...and it can contain variable that can only refer to
Sample Object..but at this situation since you have not initialize those variable..so all the four variable in array Object are null..that means they are not referrring to any thing..

this code will create 5 Object :

Sample[] s = {new Sample(),new Sample(),new Sample(),new Sample()};

here we have one array Object that hold the reference to other four sample Object...


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Thanks Sunny,
That helps remove some of confusion. Another question, references are always on the stack, while in this czse references are created on heap. So how does that work. So the actual Sample objects and references s[0[, s[1], s[2] and s[4] atre all on the heap ?
Pls reply
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by jignesh soni:
references are always on the stack.


This part is incorrect. References are not always on the stack; it's local variables -- variables declared in a method -- that are always on the stack. This is true of all local variables, whether they contain a reference or a primitive value like an int, double, or boolean.

If an object has an instance member variable that's of reference type -- i.e., like variable "a" in class Foo, here:

class Foo { String a; int b; double c; }

then when an instance of that class Foo is created on the heap, all its instance variables, a, b, and c, are all inside that object, on the heap, including that reference variable.


[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by jignesh soni:
references are always on the stack


That is not true - only local variables are on the stack, references that are fields are stored on the heap.

Think of an array as a special kind of object. In your example, the reference to the array is stored on the stack, but the array itself - and thereby the references to the objects it contains - is stored on the heap.

Does that help?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Thanks friends,
when an instance of class Foo is created, all its instance variables will be on heap. So in my example a the references in Array object will be on heap and the instances referred by these variables will also be on heap but outside Array, right ?
In your example, all the instance variables will be on heap but still reference to Foo will be on stack, right ? If Foo f = new Foo(); is created in main() then how does f become local variable ? Are variables declared inside main() are same like declared in other method ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Ilja]: That is not true - only local variables are on the stack, references that are fields are stored on the heap.

Hurm. As of JDK 6, sometimes fields and even entire objects can sometimes be stored on the stack rather than the heap, if the JVM determines through escape analysis that there's no chance any other threads can get a reference to the object. This enables it to allocate and free memory even faster, and bypass unecessary synchronization in some cases.

But since this is posted in Beginner, I suppose it would be better to just say that there's rarely any reason why most programmers need to know where their objects are being stored anyway. But for those who care: local variables are always on the stack. Fields and objects have traditionally been on the heap, but nowadays they are sometimes on the stack.

[jignesh]: when an instance of class Foo is created, all its instance variables will be on heap. So in my example a the references in Array object will be on heap and the instances referred by these variables will also be on heap but outside Array, right ?

Probably. Unless they're on the stack. From here on, I will just pretend you said "probably" every time you mention the heap.

[jignesh]: In your example, all the instance variables will be on heap but still reference to Foo will be on stack, right ?

That depends on whether the reference you're talking about is a local variable or not. I can't tell.

[jignesh]: If Foo f = new Foo(); is created in main() then how does f become local variable ? Are variables declared inside main() are same like declared in other method ?

Yes. Variable f doesn't "become" a local variable - it was a local variable the moment it was declared. There's nothing magic about main(), except that it's the standard starting point for JVM execution.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Objects
 
Similar Threads
on STACK or on HEAP?
java classes and references
java classes and references
Memory accocation in case of arrays
arrays