Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:27:52+00:00 2026-06-04T09:27:52+00:00

This might sound stupid but I am still not clear about the Java Stack

  • 0

This might sound stupid but I am still not clear about the Java Stack and the memory heap. What I know from studying is following:

1) All the method calls goes on stack.

2) All the memory allocated locally goes on memory heap (not very clear about this point)

3) All the memory allocated by new operator (either in a method or in a class) goes on memory heap.

I am worried about the below cases:

1) If I create a int variable in a method and return it, where does it go ( I believe it goes on stack, but need clarification).

2) If I create a new object in a method it goes on heap memory as it exists even after the methods execution is over(I understand this happens because the hash code of the object created by java remains same when I assign this object to some external reference variable or I return this object ).

3) My problem is what happens if I am not assigning the object mentioned in point 2 to any reference or I am not returning this. Is it still created on heap? Logically it should be but please enlighten me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:27:53+00:00Added an answer on June 4, 2026 at 9:27 am

    All method parameters go on the stack. All local variables go on the stack. The only thing that goes in the heap is stuff allocated explicitly using new (or implicitly by auto-boxing or varargs.)

    Another way to think about it is that primitive values and object/array references may go on the stack, but actual objects cannot1.

    So:

    1) – you are returning a primitive value (not a variable!), and it goes on the stack. (You can’t “return” a variable. The variable is part of the stack frame and can’t be detached from it.)

    2) Yes.

    3) Yes, at least for now1. At some point, the GC may run, notice that the application doesn’t have a reference to the object any more, and reclaim it.


    1 – actually, the latest Hotspot compilers are capable of detecting that an object’s reference never “escapes” from the method that creates it, and that the objects could be allocated on the stack. IIRC, this optimization – called escape analysis – needs to be enabled using a JVM command-line flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might sound stupid, but why doesn't the Java compiler warn about the expression
This might sound a little stupid but I am still curious about what the
I know this question might sound quite stupid, but I've tried my best and
This might sound like a stupid question, but I am about to pull hear
I know this might sound very stupid but I am a total noob in
This might sound a bit of an odd question but I know what I
I know this might sound really ridiculas but all of a sudden when I
Ok, this might sound stupid and noob but right now in my js, I
This might sound like a very stupid question to some of you, but I
This might sound like a stupid problem but I wondered for a long time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.