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 6682663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:43:57+00:00 2026-05-26T04:43:57+00:00

My Java teacher (High school course) was talking about loops and she said that

  • 0

My Java teacher (High school course) was talking about loops and she said that if you have a for loop like:

for (int i = 0; i < max; i++) {
    //something
}

you can’t use the variable i outside of the loop because the garbage collection feature deletes it because it senses that it’s “unneeded” (I know about scopes and that this is BS because the same thing happens in all languages and C++ doesn’t even have garbage collection). Now the question is… What does Garbage Collection actually do? (I looked it up and it had something to do with heaps which I don’t know about yet so someone explain this to me)

Thanks

  • 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-05-26T04:43:57+00:00Added an answer on May 26, 2026 at 4:43 am

    (I know about scopes and that this is BS because the same thing happens in all languages and C++ doesn’t even have garbage collection).

    Correct. The variable i can’t be used outside the loop because of scope — it has nothing to do with the GC (outside of potential object reach-ability).

    Now the question is… What does Garbage Collection actually do? (I looked it up and it had something to do with heaps which I don’t know about yet so someone explain this to me)

    The Garbage Collector is responsible for reclaiming objects which are no longer strongly-reachable. A garbage collector has nothing [directly] to do with variables, although a variable can keep an object strongly-reachable. (Also, primitive values, such as int are not objects are thus never dealt with by the GC 😉

    I would recommend reading through Chapter 9 of Inside the Java Virtual Machine:
    Garbage Collection
    and The Truth About Garbage Collection as I believe they will provide sufficient answers/insight and justification. (The Garbage Collection wikipedia entry is also a good start and nicely summarizes GC in general.)

    From “The Truth”:

    An object enters an unreachable state when no more strong references to it exist [is not strongly-reachable]. When an object is unreachable, it is a candidate for collection. Note the wording: Just because an object is a candidate for collection doesn’t mean it will be immediately collected. The JVM is free to delay collection until there is an immediate need for the memory being consumed by the object.

    Happy coding.

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

Sidebar

Related Questions

I was talking with my teacher, and she mentioned that an object variable (she
My teacher in an upper level Java class on threading said something that I
I am a college student at a school that teaches mainly in Java. One
Java Newbie here. I have a JFrame that I added to my netbeans project,
I'm a student in Java class and learned something about Java today that made
I have taken an AI course, and the teacher asked us to implement a
So I am in a Java class in school, and I have a relatively
I'm learning Java and I have a problem that I can't understand or solve!
My teacher gave out a practice exam on java recently and I'm curious to
Java SE 6 (64 bit only) is now on OS X and that is

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.