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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:48:34+00:00 2026-06-04T15:48:34+00:00

Today, someone attended me to bad use of the return keyword in Java. I

  • 0

Today, someone attended me to bad use of the return keyword in Java. I had written a simple for loop to validate that something is in an array. Supposing array is an array of length n, this was my code:

for (int i = 0; i < array.length; ++i) {
    if (array[i] == valueToFind) {
        return true;
    }
}
return false;

Now someone told me that this is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction. Therefore, better code would be:

int i = 0;
while (i < array.length && array[i] != valueToFind) {
    ++i;
}
return i != array.length;

The problem is that I can’t come up with a proper explenation of why the first for loop isn’t a good practice. Can somebody give me an explanation?

  • 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-04T15:48:36+00:00Added an answer on June 4, 2026 at 3:48 pm

    Now someone told me that this is not very good programming because I use the return statement inside a loop and this would cause garbage collection to malfunction.

    That’s incorrect, and suggests you should treat other advice from that person with a degree of skepticism.

    The mantra of “only have one return statement” (or more generally, only one exit point) is important in languages where you have to manage all resources yourself – that way you can make sure you put all your cleanup code in one place.

    It’s much less useful in Java: as soon as you know that you should return (and what the return value should be), just return. That way it’s simpler to read – you don’t have to take in any of the rest of the method to work out what else is going to happen (other than finally blocks).

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

Sidebar

Related Questions

Earlier today I was discussing with someone that I found out you can use
I ran into something odd today that maybe someone knows something about. I have
I was baffled today when someone mentioned on a group that Javascript has evolved
Today at work someone tried to convince me that: {$obj->getTableInfo()} is fine for smarty/mvc/templating
While cleaning some code today written by someone else, I changed the access modifier
I watched today as someone painlessly exported a NetBeans java project to a JNLP
Someone asked about Lazy Binary Searches today. Not knowing what that was, I looked
I got a hint from someone today that my curl_multi() code is actually working
I was informed by someone senior in our company today that the PHP code
Today I came across a problem where someone had accidentally committed a proj.user file

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.