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

  • Home
  • SEARCH
  • 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 8369499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:39:39+00:00 2026-06-09T13:39:39+00:00

The very simple Java code as follows has the weird output, but the same

  • 0

The very simple Java code as follows has the weird output, but the same logic code in C and C++ has the right output. I try with the JDK 1.7 and JDK 1.3 (relative JRE), the weird output is always there.

public class Test {

    public static int sum=0;

    public static int fun(int n) {

        if (n == 1)
            return 1;
        else
            sum += fun(n - 1);  // this statement leads to weird output
        // { // the following block has right output
        //     int tmp = fun(n - 1);
        //     sum += tmp;
        // }

        return sum;
    }

    public static void main(String[] arg) {
        System.out.print(fun(5));
    }
}

The output is 1 which should be 8. Relative C/C++ code is as follows:

#include<stdio.h>
int sum=0;
int fun(int n) {

        if (n == 1)
            return 1;
        else
            sum += fun(n - 1);

        return sum;
    }

int main()
{
    printf("%d",fun(5));

    return 0;
}

Adding test java code:

class A {
    public int sum = 0;

    public int fun(int n) {
        if(n == 1) {
            return 1;
        } else {
            sum += fun(n - 1);
            return sum;
        }
    }
}

public class Test {
    public static void main(String arg[]){
        A a = new A();
        System.out.print(a.fun(5));
    }
}
  • 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-09T13:39:40+00:00Added an answer on June 9, 2026 at 1:39 pm

    I’m going to run through this for fun(3) for the sake of giving a complete answer. For those of you who are not interested why this works for C++ but not for Java, please ignore my answer.

    Here is what Java is doing:

    inside fun(3)

    sum += sum + fn(n-1) // sum is 0
    

    becomes

    sum = 0 + fun(2) // sum is 0
    

    Then inside fun(2)

    sum = 0 + fun(1) // sum is 0
    

    Then inside fun(1)

    return 1 // sum is 0
    

    Back inside fun(2)

    sum = 0 + 1; // sum is 0
    

    becomes

    sum = 1; // sum will soon become 1
    

    Back inside fun(3)

    sum = 0 + 1; // sum is 1
    

    becomes

    sum = 1; // sum gets reset to 1
    

    Here is what C++ is doing:

    inside fun(3)

    sum += fn(n-1) // sum is 0
    

    becomes

    sum = sum + fn(2) // sum is 0
    

    Then inside fun(2)

    sum = sum + fn(1) // sum is 0
    

    Then inside fun(1)

    return 1 // sum is 0
    

    Back inside fun(2)

    sum = sum + 1 // sum is 0
    

    Becomes

    sum = 0 + 1 => sum = 1 // sum will soon become 1
    

    Back inside fun(3)

    sum = sum + 1 // sum is 1
    

    Becomes

    sum = 1 + 1 // sum will soon become 2
    

    What you should do:
    I do not know why C++ evaluates sum after making the function call rather than before. I do not know if this is in the specifications. But I do know that you should not be depending on this in any language. A correct solution would be:

    int fun(int n) {
        if (n == 1)
            return 1;
        else
            return n + f(n - 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Java code is a very simple piece of code, but what are
A very simple Vector in Java that produces the output that is somewhat difficult
I have a very simple Java code like this. I don't have any idea
I have a very simple client server code written java(server listens on some port
Say I have a very simple java object that only has some getXXX and
A very simple question but i'm newbie in Java. How can I do the
I am planning to develop a very simple java application (not mobile, but desktop
I have written a very simple java script code. I want to change the
I'm building a very simple Java parser, to look for some specific usage models.
I'm working on converting a very simple java desktop application to run in java

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.