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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:18:24+00:00 2026-06-12T17:18:24+00:00

I came across the following issue: private void doStuff(int i) { if(i>10) { return;

  • 0

I came across the following issue:

private void doStuff(int i) {
   if(i>10) {
      return;
   }
   doStuff(i++); 
}

public void publicMethod() {
   doStuff(i);
}

I would expect this to run doStuff 10 times and then return.

However i++ does not get executed before the doStuff is called again with 0.

Result is an infinite loop. I know how to fix it but I am wondering if this behaviour is correct or a bug.

  • 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-12T17:18:26+00:00Added an answer on June 12, 2026 at 5:18 pm

    Now I would expect this to run doStuff 10 times and then return, however i++ does not get execute before the doStuff is called again with 0.

    Yes, the result of the post-increment operator is the original value… and then inside the next call to the method you’ve a new i. So in other words, this call:

    doStuff(i++); 
    

    is equivalent to:

    int original = i;
    i = original + 1;
    doStuff(original);
    

    From JLS section 15.14.2:

    The value of the postfix increment expression is the value of the variable before the new value is stored.

    Given that you don’t use i again afterwards (and thus any side-effect on it is pointless), why not just simplify your life?

    doStuff(i + 1);
    

    (As with all parameters in Java, you’re seeing pass-by-value – changing the value of i in the method does not change the value of the caller’s argument.)

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

Sidebar

Related Questions

I came across the following issue: I have this model public class Region {
When running some tests I came across the following issue. When using: private String
I came across the following code : int i; for(; scanf(%s, &i);) printf(hello); As
I came across this post on the MSMobiles.com blog that says the following, among
I came across the following issue: I have a main simple page on which
Just started to learn C and came across the following issue: I need to
While familiarizing myself with AJAX, I came across the following issue I do not
Writing my first PHP Class and came across an issue, the following is my
I'm using iText to generate PDF reports when I came across this issue, and
I came across following code and don't know what does having from twice mean

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.