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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:03:24+00:00 2026-05-27T00:03:24+00:00

How can I swap the contents of two Integer wrappers? void swap(Integer a,Integer b){

  • 0

How can I swap the contents of two Integer wrappers?

void swap(Integer a,Integer b){
   /*We can't use this as it will not reflect in calling program,and i know why
    Integer c = a;
    a= b;
    b = c;
   */
  //how can i swap them ? Does Integer has some setValue kind of method?
  //if yes
    int c = a;
    a.setValue(b);
    b.setValue(c);

}
  • 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-27T00:03:25+00:00Added an answer on May 27, 2026 at 12:03 am

    You can’t, precisely because Integer is immutable (along with the other primitive wrapper types). If you had a mutable wrapper class, it would be fine:

    public final class MutableInteger {
    {
        private int value;
    
        public MutableInteger(int value) {
            this.value = value;
        }
    
        public void setValue(int value) {
            this.value = value;
        }
    
        public int getValue() {
            return value;
        }
    }
    
    void swap(MutableInteger a, MutableInteger b) {
        int c = a.getValue();
        a.setValue(b.getValue());
        b.setValue(c); 
    }
    

    However, due to the lack of the equivalent of setValue in Integer, there’s basically no way of doing what you’re asking. That’s a good thing. It means that for most cases, where we may want to pass an Integer value to another method, we don’t need to worry about whether the method will mutate it. Immutability makes it much easier to reason about your code, without having to carefully trace what every method does, just in case it changes your data under your feet.

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

Sidebar

Related Questions

I am trying to 'swap' two cells' contents, and their mappings. To do this,
a=b+(b=a)*0; This sentence can swap the value between a and b. I've tried it
$(tr #contactus_+id).before($(tr #contactus_+id).prev()); I know you can swap the position of the tr tags
Based on pp. 8 Free Functions template<typename T> void swap(scoped_ptr<T>& a,scoped_ptr<T>& b) This function
ArrayBuffer extends ResizableArray which include a protected method, swap. Yet I can't access swap.
How can I do a sed regex swap on all text that preceed a
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can I get a 'when to use' for these and others? <% %> <%#
I receive encoded PDF files regularly. The encoding works like this: the PDFs can
So I know that it is easy enough to swap images of named img

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.