• 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Question on Assignments

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone know if the String [] args variable in main method be counted as a variable stored on the stack ?
[ October 02, 2007: Message edited by: Rama Yannam ]
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rama,

Does anyone know if the String [] args variable in main method be counted as a variable stored on the stack ?


The String []args is an array of strings with array name as "args". And as its an array so it lands at Heap and not Stack. Hope this is what you are asking?
 
Rama Yannam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pranav for your reply.

But still, args local variable pointing to an object, in this case an arry of sting.

Rama
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that all method variables (local variables) are considered stack objects. since String[] args is a local variable, then i think it is placed in the stack.
 
Rama Yannam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading the Cathy Sierra Bert Bates 5.0 certification book, and in the Assignment chapter, the first example that talks about heap and stack seems to ignore this. Hope this is a typo.. or is it not ??
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rama,
In this case i think that a local variable args will be on stack but it may or may not point to any String Object as the argument may be furnished at run time or it may not. I think more clarification can be given by Bert or Kathy themselves...
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Not specifically knowing the answer, but experience giving me this gut feeling, my response would be which stack?

Of course, we're talking about the stack referred to in K&B, but there's (probably) some behind the scenes action going on as well.

I believe the JVM maintains its own stack and heap for what's discussed in the book. However, there are more stacks behind the scenes. Our stack doesn't come into context until main() starts. True, args is probably an array on the heap, but pass by value says that the value of args refers to the heap, and is not on the heap itself. When you access args, it is pulled off the stack to determine where the heap location is. The JVM creates an object on the heap automatically between when the JVM fires up and when main() starts.

That said, now a different stack... Take a mental step back and think of an operating system or cpu stack that is separate from the JVM stack. Think of it like this: When a method is called, the JVM pushes the arguments of the calling method onto the os/cpu stack. When the called method is invoked, it pops those parameters from that stack and places them on the JVM stack to keep the world spinning . After that, the parameters are at the mercy of you - the programmer.

Similarly, when you return a value from a method, I believe it is pushed onto the os/cpu stack and popped off by the calling function. It is then either assigned to a variable or sent to GC.

That's a traditional method of how I know how it works. If those of the knowhow can shed a different light on this, I'd be curious (and humbly corrected) to know myself.

Aloha,
Doug
 
Rama Yannam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Doug for your detailed explanation.
I guess the question still remains on weather it was a typo in the book or there is another side to it.

-Rama
 
What are your superhero powers? Go ahead and try them on this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic