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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:06:08+00:00 2026-06-06T07:06:08+00:00

I am using recursion and I want an Integer object to retain its value

  • 0

I am using recursion and I want an Integer object to retain its value across recursive calls. e.g.

public void recursiveMethod(Integer counter) {
    if (counter == 10)
        return;
    else {
        counter++;
        recursiveMethod(counter);
    }
}

public static void main(String[] args) {
    Integer c = new Integer(5);
    new TestSort().recursiveMethod(c);
    System.out.println(c); // print 5
}

But in the below code (where I am using a Counter class instead of Integer wrapper class, the value is maintained

public static void main(String[] args) {
    Counter c = new Counter(5);
    new TestSort().recursiveMethod(c);
    System.out.println(c.getCount()); // print 10
}

public void recursiveMethod(Counter counter) {
    if (counter.getCount() == 10)
        return;
    else {
        counter.increaseByOne();
        recursiveMethod(counter);
    }
}

class Counter {

    int count = 0;

    public Counter(int count) {
        this.count = count;
    }

    public int getCount() {
        return this.count;
    }

    public void increaseByOne() {
        count++;
    }
}

so why primitve wrapper class behaves differently. After all, both are objects and in the reucrsive call, I am passing the Integer object and not just int so that Integer object must also maintain its value.

  • 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-06T07:06:09+00:00Added an answer on June 6, 2026 at 7:06 am

    The Java wrapper types are immutable. Their values never change.

    counter++ is really counter = counter + 1; i.e. a new object gets created.

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

Sidebar

Related Questions

I'm doing some generic object comparison, using reflection and recursion. The recursive method needs
I want to create a PHP recursive program using a Binary Tree and Recursion.
I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a
Im trying to create a list of strings using some recursion. Basically i want
I want to calculate the power of 2 using recursion. Here is my code:
I want to find all excel files within a directory structure using recursion. The
I want to sum the numbers in a list without using recursion. I know
I want to program a function to find C(n,k) using tail recursion, and I
I'm using pyparsing to parse vcd (value change dump) files. Essentially, I want to
Using recursion, find an index that cuts an array in two parts so that

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.